-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Enum lookup for map type [P in Enum] gives "implicit any index" error #19546
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
Comments
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 |
I think my confusion lies with how the compiler actually "sees" the 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... |
I've also found this very counterintuitive. The workaround was more verbose but was expressive and allowed for very convenient consumption. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
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
All of the following work:
Expected behavior:
map[color]
to work and give return type ofnumber
Actual behavior:
map[color] // Error: Element implicitly has an 'any' type because type 'ColorMap' has no index
The text was updated successfully, but these errors were encountered: