You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enum lookup for map type [P in Enum] gives "implicit any index" error #19546
It could be I stumbled on something I simply don't understand, but it seems like this should work:
Code
enumColor{RED="RED",BLUE="BLUE",GREEN="GREEN"}typeColorMap={[PinColor]: number;// type ColorMap = { RED: number; BLUE: number; GREEN: number; }}declareconstcolor: Color;map[color]// Error: Element implicitly has an 'any' type because type 'ColorMap' has no index signature.
All of the following work:
declareconstmap: ColorMap;map[Color.RED]// OKconstred: Color=Color.RED;map[red]// OKdeclareconstcolor: keyoftypeofColor;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
declareconstcolor: Color.RED|Color.BLUE|Color.GREEN;map[color]// Error: Element implicitly has an 'any' type because type 'ColorMap' has no index signature.declareconstcolor: Color.RED|Color.BLUE|"GREEN";map[color]// OK
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:
typeColor="RED"|"BLUE"|"GREEN"namespaceColor{exportconstRED="RED";exportconstBLUE="BLUE";exportconstGREEN="GREEN";}typeColorMap={[PinColor]: number;}declareconstmap: ColorMap;map[Color.RED]// OKconstred: Color=Color.RED;map[red]// OKdeclareconstcolor: Color;map[color]// Ok
But it obviously is a different thing that isn't as intuitive to me...
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
aaronbeall commentedon Oct 28, 2017
More stuff I don't understand:
aaronbeall commentedon Oct 28, 2017
I think my confusion lies with how the compiler actually "sees" the
enumtype... I thought that string enum was equivalent to the old string literaltype+namespaceworkaround:But it obviously is a different thing that isn't as intuitive to me...
aluanhaddad commentedon Oct 28, 2017
I've also found this very counterintuitive. The workaround was more verbose but was expressive and allowed for very convenient consumption.
[-]Enum lookip[/-][+]Enum lookup for map type [P in Enum] gives "implicit any index" error[/+]mhegazy commentedon Oct 30, 2017
Duplicate of #16760. Should be fixed by #18029
typescript-bot commentedon Nov 13, 2017
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.