Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zhuravlikjb opened this issue Jan 22, 2015 · 3 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this

Comments

@zhuravlikjb
Copy link

"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);
}
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 22, 2015
@mhegazy mhegazy added this to the Community milestone Jan 22, 2015
@mhegazy mhegazy added the Help Wanted You can do this label Jan 22, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Jan 22, 2015

A PR would be appreciated

@jbondc
Copy link
Contributor

jbondc commented Feb 26, 2015

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

@mhegazy mhegazy closed this as completed Mar 8, 2015
@iamisti
Copy link

iamisti commented Mar 16, 2016

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]

@mhegazy mhegazy removed this from the Community milestone Apr 26, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

5 participants