【发布时间】:2020-12-26 18:20:23
【问题描述】:
如何在 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'
}
【问题讨论】:
-
您可以像@Mark Skelton 提到的那样做,也可以通过
this.doCalculation(FooType[foo])简单地遍历数组(枚举类型)
标签: typescript enums