Skip to content

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

@aaronbeall

Description

@aaronbeall

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions