如何在 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
}
Run Code Online (Sandbox Code Playgroud)
枚举FooType看起来像这样:
export enum SupportedFiat {
VALUE_A = 'VALUE_A',
VALUE_B = 'VALUE_B',
VALUE_C = 'VALUE_C'
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ton 14
您应该能够使用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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11462 次 |
| 最近记录: |