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

Enum lookup for map type [P in Enum] gives "implicit any index" error #19546

Closed
aaronbeall opened this issue Oct 28, 2017 · 5 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@aaronbeall
Copy link

aaronbeall commented Oct 28, 2017

TypeScript Version: 2.5.3

It could be I stumbled on something I simply don't understand, but it seems like this should work:

Code

enum Color {
  RED = "RED",
  BLUE = "BLUE",
  GREEN = "GREEN"
}

type ColorMap = {
  [P in Color]: number; // type ColorMap = { RED: number; BLUE: number; GREEN: number; }
}

declare const color: Color;
map[color] // Error: Element implicitly has an 'any' type because type 'ColorMap' has no index signature.

All of the following work:

declare const map: ColorMap;
map[Color.RED] // OK

const red: Color = Color.RED;
map[red] // OK

declare const color: keyof typeof Color;
map[color] // OK -- I don't know why this works

Expected behavior:

map[color] to work and give return type of number

Actual behavior:

map[color] // Error: Element implicitly has an 'any' type because type 'ColorMap' has no index

@aaronbeall
Copy link
Author

aaronbeall commented Oct 28, 2017

More stuff I don't understand:

declare const color: Color.RED | Color.BLUE | Color.GREEN;
map[color] // Error: Element implicitly has an 'any' type because type 'ColorMap' has no index signature.

declare const color: Color.RED | Color.BLUE | "GREEN";
map[color] // OK

@aaronbeall
Copy link
Author

aaronbeall commented Oct 28, 2017

I think my confusion lies with how the compiler actually "sees" the enum type... I thought that string enum was equivalent to the old string literal type + namespace workaround:

type Color = "RED" | "BLUE" | "GREEN"
namespace Color { export const RED = "RED"; export const BLUE = "BLUE"; export const GREEN = "GREEN"; }

type ColorMap = {
  [P in Color]: number;
}

declare const map: ColorMap;
map[Color.RED] // OK

const red: Color = Color.RED;
map[red] // OK

declare const color: Color;
map[color] // Ok

But it obviously is a different thing that isn't as intuitive to me...

@aluanhaddad
Copy link
Contributor

I've also found this very counterintuitive. The workaround was more verbose but was expressive and allowed for very convenient consumption.

@aaronbeall aaronbeall changed the title Enum lookip Enum lookup for map type [P in Enum] gives "implicit any index" error Oct 28, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 30, 2017

Duplicate of #16760. Should be fixed by #18029

@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 30, 2017
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants