2

I have a type alias that uses a union of literal types for the key:

type Type = {
  [key in 'a' | 'b']: string
}

However, TypeScript fails to infer the type correctly when I do this:

const object: Type = {'a': 'aaa', 'b': 'bbb'}

Object.keys(object).forEach(key => ...)

The type of key above is inferred as string, although I would've expected it to be inferred as 'a' | 'b'.

Here's a reproduction of the above.

artooras
  • 6,315
  • 9
  • 45
  • 78

0 Answers0