如何遍历 TypeScript 中的枚举项?我试过 for-in,但这会遍历字符串。我需要为每个枚举值调用一个函数。
for (const foo in FooType) {
// here I have error that string is not assignable to parameter of type FooType
this.doCalculation(foo)
}
private doCalculation(value: FooType): void {
// some logic
}
枚举 FooType
看起来像这样:export enum SupportedFiat {
VALUE_A = 'VALUE_A',
VALUE_B = 'VALUE_B',
VALUE_C = 'VALUE_C'
}
最佳答案
您应该可以使用 for of
来完成此操作。和 Object.values
.
for (const value of Object.values(FooType)) {
// here I have error that string is not assignable to parameter of type FooType
doCalculation(value)
}
关于typescript - 迭代 Typescript 中的枚举项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65459359/