typescript - 迭代 Typescript 中的枚举项

标签 typescript enums

如何遍历 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/

相关文章:

javascript - Angular 10 |邮寄 |字符串数组追加 FormData

typescript - 类型 'target' 上不存在属性 'KeyboardEventHandler<HTMLInputElement>'

javascript - TypeScript 中的 AngularJS 值

c++ - 即使包含枚举也无法识别

jsp - 在JSP中将枚举值作为标记属性传递

c# - 在程序执行期间更改枚举描述

typescript - 如何在每个文件的基础上选择加入 TypeScript 的 strictPropertyInitialization?

c++ - 使用枚举重载函数

c++ - 以编程方式独立于其元素的值推断枚举的大小?

javascript - 共享前端和后端的类型定义