0

I am trying to use an integer Enum as a key for an object type and it generates the error I included in the comments:

enum Enum {
  first = 1,
  second = 2,
  third = 3,
}

type EnumAsKey = {
  [key in Enum]: string; //<-- Type 'Enum' is not assignable to type 'string'
};

Shouldn't Enum be assignable to type number and can't a number be used as an index here?

How do I achieve this?

I have also tried:

[key: Enum] <- An index signature parameter type must be 'string', 'number', 'symbol', or a template literal type.
[key: typeof Enum[keyof typeof Enum]] <-- Type 'Enum' is not assignable to type 'string'

Related, but this solution only appears to work when the enum is of strings: How to use enum as index key type in typescript?

2
  • 2
    Can you provide a playground link? I can't reproduce the error here Commented Jun 22, 2023 at 16:54
  • Strange! I'll have to investigate whether this is a case of the repository using an older version of TS and will update as appropraite. Commented Jun 22, 2023 at 17:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.