Typescript: “ const”枚举中的索引表达式参数必须为s /类型为“字符串” /字符串文字/

创建于 2015-01-22  ·  3评论  ·  资料来源: microsoft/TypeScript

“'const'枚举中的索引表达式参数必须为'string'类型”

此错误消息是非常模棱两可的:
type_string

似乎应该
“'const'枚举的索引表达式参数必须是字符串文字”

因为编译器这样做:

if (isConstEnumObjectType(objectType) && node.argumentExpression && node.argumentExpression.kind !== SyntaxKind.StringLiteral) {
  error(node.argumentExpression, Diagnostics.Index_expression_arguments_in_const_enums_must_be_of_type_string);
}
Bug help wanted

最有用的评论

如果有人遇到相同的问题,只需尝试删除“ const”,一切都将正常工作。
嗯,这并非完全正确,您必须执行以下操作:

enum MyEnum{ 
    A, B, C
}

let index = 2;

// this is how you can get the value from the enum without haveing tslint error message.
(<any>MyEnum)[index]

所有3条评论

公关将不胜感激

现在的消息是“只能使用字符串文字访问const枚举成员。”对我来说看起来不错。

如果有人遇到相同的问题,只需尝试删除“ const”,一切都将正常工作。
嗯,这并非完全正确,您必须执行以下操作:

enum MyEnum{ 
    A, B, C
}

let index = 2;

// this is how you can get the value from the enum without haveing tslint error message.
(<any>MyEnum)[index]
此页面是否有帮助?
0 / 5 - 0 等级