我想迭代一个TypeScript枚举对象,并获得每个枚举符号名称,例如: enum myEnum {entry1, entry2}
for (var entry in myEnum) {
// use entry's name here, e.g., "entry1"
}
我想迭代一个TypeScript枚举对象,并获得每个枚举符号名称,例如: enum myEnum {entry1, entry2}
for (var entry in myEnum) {
// use entry's name here, e.g., "entry1"
}
当前回答
这里有很多答案,尽管这是一个7年前的问题,但我还是查了一下,我猜会有更多的答案出现在这里。这是我的解决方案,它比其他解决方案简单一点,它处理数字/文本/混合值枚举,都是一样的。
enum funky {
yum , tum='tum', gum = 'jump', plum = 4
}
const list1 = Object.keys(funky)
.filter(k => (Number(k).toString() === Number.NaN.toString()));
console.log(JSON.stringify(list1)); // ["yum","tum","gum","plum"]"
// for the numeric enum vals (like yum = 0, plum = 4), typescript adds val = key implicitly (0 = yum, 4 = plum)
// hence we need to filter out such numeric keys (0 or 4)
其他回答
让ts-enum-util (github, npm)为您工作,并提供许多额外的类型安全实用程序。适用于字符串和数字enum,正确忽略数字enum的数字索引反向查找条目:
字符串枚举:
import {$enum} from "ts-enum-util";
enum Option {
OPTION1 = 'this is option 1',
OPTION2 = 'this is option 2'
}
// type: ("OPTION1" | "OPTION2")[]
// value: ["OPTION1", "OPTION2"]
const keys= $enum(Option).getKeys();
// type: Option[]
// value: ["this is option 1", "this is option 2"]
const values = $enum(Option).getValues();
数字枚举:
enum Option {
OPTION1,
OPTION2
}
// type: ("OPTION1" | "OPTION2")[]
// value: ["OPTION1", "OPTION2"]
const keys= $enum(Option).getKeys();
// type: Option[]
// value: [0, 1]
const values = $enum(Option).getValues();
我通过搜索“TypeScript iterate over enum keys”找到了这个问题。所以我只想给出对我来说有用的解。也许对别人也有帮助。
我的情况如下:我想在每个枚举键上迭代,然后过滤一些键,然后访问一些对象,其中键作为枚举的计算值。这就是没有TS误差的方法。
enum MyEnum = { ONE = 'ONE', TWO = 'TWO' }
const LABELS = {
[MyEnum.ONE]: 'Label one',
[MyEnum.TWO]: 'Label two'
}
// to declare type is important - otherwise TS complains on LABELS[type]
// also, if replace Object.values with Object.keys -
// - TS blames wrong types here: "string[] is not assignable to MyEnum[]"
const allKeys: Array<MyEnum> = Object.values(MyEnum)
const allowedKeys = allKeys.filter(
(type) => type !== MyEnum.ONE
)
const allowedLabels = allowedKeys.map((type) => ({
label: LABELS[type]
}))
如果你想在html中获取名字
对于这个枚举
enum CanadianProvinces {
AB,
BC,
MB,
NB,
NL,
NT,
NS,
NU,
ON,
PE,
QC,
SK,
YT
}
将组件中的变量赋值为
canadianProvinces = CanadianProvinces
然后,在你的html
{{canadianProvinces[0]}}
可以是简短的:
enum AnimalEnum {
DOG = "dog",
CAT = "cat",
MOUSE = "mouse"
}
Object.keys(AnimalEnum).filter(v => typeof v == 'string' && isNaN(v))
我卑微的2美分基于阅读一个了不起的评论从github TS讨论
const EnvironmentVariants = ['development', 'production', 'test'] as const
type EPredefinedEnvironment = typeof EnvironmentVariants[number]
然后在编译时:
// TS2322: Type '"qaEnv"' is not assignable to type '"development" | "production" | "test"'.
const qaEnv: EPredefinedEnvironment = 'qa'
在运行时:
function isPredefinedEnvironemt(env: string) {
for (const predefined of EnvironmentVariants) {
if (predefined === env) {
return true
}
}
return false
}
assert(isPredefinedEnvironemet('test'), true)
assert(isPredefinedEnvironemet('qa'), false)
注意,for(const index in environmentvariables){…}将遍历"0","1","2"集合