获取字符串文字类型值的数组 [英] Get array of string literal type values

查看:32
本文介绍了获取字符串文字类型值的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取字符串文字的所有可能值的完整列表.

I need to get a full list of all possible values for a string literal.

type YesNo = "Yes" | "No";
let arrayOfYesNo : Array<string> = someMagicFunction(YesNo); //["Yes", "No"]

有没有办法做到这一点?

Is there any way of achiving this?

推荐答案

Typescript 2.4 是 终于发布.在字符串枚举的支持下,我不再需要使用字符串文字,并且因为 typescript 枚举被编译为 javascript,我可以在运行时访问它们(与 数字枚举).

Typescript 2.4 is finally released. With the support of string enums I no longer have to use string literals and because typescript enums are compiled to javascript I can access them at runtime (same approach as for number enums).

这篇关于获取字符串文字类型值的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆