Typescript: Index expression arguments in 'const' enums must be s/of type 'string'/a string literal/

Created on 22 Jan 2015  ·  3Comments  ·  Source: microsoft/TypeScript

"Index expression arguments in 'const' enums must be of type 'string'"

This error message is quite ambiguous:
type_string

Seems that it should be
"Index expression argument of 'const' enum must be a string literal"

Because the compiler does so:

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

Most helpful comment

If somebody has the same problem, just try to remove 'const' and everything will just work.
Well it's not entirely true, you have to do something like:

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]

All 3 comments

A PR would be appreciated

The message is now "A const enum member can only be accessed using a string literal.", looks good to me.

If somebody has the same problem, just try to remove 'const' and everything will just work.
Well it's not entirely true, you have to do something like:

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]
Was this page helpful?
0 / 5 - 0 ratings