【发布时间】:2019-11-17 16:36:21
【问题描述】:
我尝试使用枚举值作为数组的索引,但它给了我一个错误。
export class Color {
static RED = 0;
static BLUE = 1;
static GREEN = 2;
}
let x = ['warning', 'info', 'success'];
let anotherVariable = x[Color.RED]; <---- Error: Type 'Color' cannot be used as an index type.
我尝试了 Number() 和 parseInt 来转换为数字,但它不起作用。
有什么方法可以将枚举值用作索引?
【问题讨论】:
-
那不是枚举。而
Color.RED不是Color的实例。 -
如果我删除 export 关键字,在 Chrome 中的纯 JS 中对我有用
-
@mplungjan 在这里也一样。但不是在 Firefox 中 - 它报告尚不支持字段。
-
只有当你有
x[Color]而不是x[Color.RED]Typescript Playground时才会显示这个错误
标签: javascript arrays enums array-indexing