简体   繁体   English

将打字稿字符串文字类型转换为字符串数组

[英]Coverting typescript string literal types to an array of string

I have我有

type ImageVerticalSpacing = 'ignoreBottom' | 'ignoreTop' | 'ignoreBoth' 
| 'Default';

in typescript and need to pass those strings as an array of strings to a dropdown.在打字稿中,需要将这些字符串作为字符串数组传递给下拉列表。 How can I convert type ImageVerticalSpacing to an array of strings?如何将 ImageVerticalSpacing 类型转换为字符串数组?

You can't convert types in TypeScript to values at runtime. 您无法在运行时将TypeScript中的类型转换为值。 But you can do the reverse: create a runtime object and have TypeScript infer its type. 但是你可以反过来:创建一个运行时对象并让TypeScript推断它的类型。

The ideal runtime object for this purpose would be a tuple . 用于此目的的理想运行时对象将是元组 Unfortunately, TypeScript doesn't infer tuples that well by itself. 不幸的是,TypeScript本身并不能很好地推断元组。 I use a helper function called tuple() which returns tuple types. 我使用一个名为tuple()的辅助函数来返回元组类型。

UPDATE: 2018-12, since TypeScript 3.0 the tuple() function can be written like this: 更新:2018-12,自TypeScript 3.0起, tuple()函数可以像这样编写:

type Narrowable = string | number | boolean | symbol | 
  object | {} | void | null | undefined;
const tuple = <T extends Narrowable[]>(...args: T)=>args;

Using the above helper function, you can do this: 使用上面的帮助函数,您可以这样做:

const imageVerticalSpacing = tuple('ignoreBottom','ignoreTop','ignoreBoth','Default');

type ImageVerticalSpacing = (typeof imageVerticalSpacing)[number];

The imageVerticalSpacing object is an array of strings you can use for your dropdown, of type ['ignoreBottom','ignoreTop','ignoreBoth','Default'] . imageVerticalSpacing对象是一个可用于下拉列表的字符串数组,类型为['ignoreBottom','ignoreTop','ignoreBoth','Default'] And the type ImageVerticalSpacing is the same 'ignoreBottom' | 'ignoreTop' | 'ignoreBoth' | 'Default' ImageVerticalSpacing类型与'ignoreBottom' | 'ignoreTop' | 'ignoreBoth' | 'Default'相同 'ignoreBottom' | 'ignoreTop' | 'ignoreBoth' | 'Default' 'ignoreBottom' | 'ignoreTop' | 'ignoreBoth' | 'Default' as you declared. 你声明的'ignoreBottom' | 'ignoreTop' | 'ignoreBoth' | 'Default'

(See it in action on The Playground ) (参见游乐场的行动)

Hope that helps. 希望有所帮助。 Good luck! 祝好运!

TypeScript interfaces or type aliases don't exist at runtime. 运行时不存在TypeScript接口或类型别名。 So you cannot use the values from them at runtime. 因此,您无法在运行时使用它们中的值。 You can try using an enum: 您可以尝试使用枚举:

enum ImageVerticalSpacing  {
    ignoreBottom,
    ignoreTop,
    ignoreBoth,
    Default
}

Object.keys(ImageVerticalSpacing).filter(k => isNaN(k as any)) // The filter is to filter out the index keys that TypeScript generates. See the generated JS code
// ["ignoreBottom", "ignoreTop", "ignoreBoth", "Default"]

Playground link 游乐场链接

Building on top of jcal's answer and reiterating: "You can't convert types in TypeScript to values at runtime. But you can do the reverse: create a runtime object and have TypeScript infer its type."建立在jcal 的回答之上并重申:“您不能在运行时将 TypeScript 中的类型转换为值。但您可以反过来:创建一个运行时对象并让 TypeScript 推断其类型。” There is a great blog post by @steve-holgado to use const assertions for this purpose: https://steveholgado.com/typescript-types-from-arrays/ @steve-holgado 有一篇很棒的博客文章,用于为此目的使用常量断言: https ://steveholgado.com/typescript-types-from-arrays/

With typescript's const assertions (TypeScript 3.4+) you can do:使用 typescript 的const 断言(TypeScript 3.4+),您可以执行以下操作:

const animals = ['cat', 'dog', 'mouse'] as const
type Animal = typeof animals[number]

// type Animal = 'cat' | 'dog' | 'mouse'

So the question's code would look like:所以问题的代码看起来像:

const imageVerticalSpacing = ['ignoreBottom', 'ignoreTop', 'ignoreBoth', 'Default'];
type ImageVerticalSpacing = typeof imageVerticalSpacing[number];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM