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?

BobtheMagicMoose
  • 2,246
  • 3
  • 19
  • 27
  • 2
    Can you provide a playground link? I can't reproduce the error [here](https://www.typescriptlang.org/play?ts=5.0.4#code/KYOwrgtgBAou0G8BQVVQGYEsBOBnALlALxQCMANCmrsAMYD2IAJsVAEyVpT4AWOLJAMyUAvkiT4AngAdgseAEFcAaWCTWyLgG0A1mqiYQ8yAF0AXFALZDAcwDcUAPSOAPAFo3UACoy5AcjhIPwNcKBB6QgBDXFxMGxBIgCMAGzl8em5fKD8rWz8kETskIA) – wonderflame Jun 22 '23 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. – BobtheMagicMoose Jun 22 '23 at 17:06

0 Answers0