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

Querying Enums containing string initializers with a variable throws TS7015 #17800

Closed
CSchulz opened this issue Aug 15, 2017 · 6 comments
Closed
Labels
Bug A bug in TypeScript
Milestone

Comments

@CSchulz
Copy link

CSchulz commented Aug 15, 2017

TypeScript Version: 2.4.2

Code

enum Colors {
  Red = 'RED',
  Green = 'GREEN',
  Blue = 'BLUE',
}
const test: string = 'Red';
Colors[test];

Workaround

enum Colors {
  Red = 'RED',
  Green = 'GREEN',
  Blue = 'BLUE',
}
const test: any = 'Red';
Colors[test];

Expected behavior:
Should compile

Actual behavior:

error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.

There is an issue #11773 demonstrating it with a "normal" enum.

@ajafff
Copy link
Contributor

ajafff commented Aug 15, 2017

just remove the type annotation:

const test = 'Red';
// alternative
const test: keyof Colors = 'Red';

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 15, 2017
@RyanCavanaugh
Copy link
Member

Seems we never did anything different with the indexing rules for string enums, which is bad. Colors[32] is extremely suspect.

@mhegazy mhegazy added this to the TypeScript 2.7 milestone Nov 2, 2017
@sandersn sandersn added this to Not started in Rolling Work Tracking Nov 8, 2017
@fmpierce
Copy link

fmpierce commented Nov 17, 2017

I found the fix!

const test: keyof typeof Colors = 'Red';

I had the same problem and that fixed it.

@duluca
Copy link

duluca commented Dec 5, 2017

I've a different repro case.

Playground link: https://www.typescriptlang.org/play/index.html#src=enum%20ContentType%20%7B%0D%0A%20%20Article%20%3D%20'a'%2C%0D%0A%20%20Video%20%3D%20'v'%0D%0A%7D%0D%0A%0D%0Aconst%20args%20%3D%20%7B%20type%3A%20%5B%20'Article'%2C%20'Video'%20%5D%20%7D%0D%0A%0D%0Awindow.alert(args.type.map(t%20%3D%3E%20ContentType%5Bt%5D))

Source:

enum ContentType {
  Article = 'a',
  Video = 'v'
}

const args = { type: [ 'Article', 'Video' ] }

window.alert(args.type.map(t => ContentType[t]))

Workaround:
As @fmpierce suggests, rewriting t as (t: keyof typeof ContentType) does fix the compilation issue. Obviously, the amount of code that needs to be written for the workaround is unacceptable.

Expected behavior:
The compiler should be able to figure out that the intent is keyof typeof ContentType.

Actual behavior:

error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.

@mhegazy mhegazy modified the milestones: TypeScript 2.7, TypeScript 2.8 Jan 9, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.8, TypeScript 2.9 Mar 9, 2018
@mhegazy mhegazy modified the milestones: TypeScript 3.0, Future Jul 2, 2018
@hotdang-ca
Copy link

Thanks, @fmpierce! Used your solution in the following:

Object.keys(Colors).map((color: keyof typeof Color): JSX.Element => {
    return (
        <div
            key={color}
            onClick={() => this._methodThatTakesAnEnum(Color[color])}
        >
            { Util.functionThatTakesAString(color) }
        </div>
    );
});

The anon function signature color: string causes the same error when trying to access Color[color].

@sandersn sandersn removed their assignment Jan 7, 2020
@RyanCavanaugh
Copy link
Member

I don't really see this as a bug at this point - enums don't have index signatures, it'd be a huge break to add one now, and it doesn't seem to merit a special case in the indexing rules.

@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
Rolling Work Tracking
  
Not started
Development

No branches or pull requests

8 participants