Skip to content

{ [key in keyof typeof Enum]: any } is not index signature #20011

@CzBuCHi

Description

@CzBuCHi

TypeScript Version: 2.4.1

Code

enum Name {
  Fred, Vilma
}
type Names = keyof typeof Name; // type Names = "Fred" | "Vilma";
type Data = ...;

const data: { [name in Names]: Data } = {
  Fred: { ... },
  Vilma: { ... },
}

function getData(name: Names) {
  return data[strName]; // error here
}

Expected behavior:
No error
Actual behavior:
Compilation error message:
Element implicitly has an 'any' type because type '{ readonly Fred: Data; readonly Vilma: Data; ...' has no index signature.

can be fixed by casting data to any or to { [name: string]: Data } before using indexer

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions