26

I've got an object like this

const MY_OBJECT = {
  'key': 'key val',
  'anotherKey': 'anotherKey val',
};

Is there a way to extract from this object 'key' | 'anotherKey' type ?

Umbrella
  • 1,085
  • 1
  • 14
  • 30

1 Answers1

42

To get a type that is a union keys of a variable you need to use keyof typeof variableName.

const MY_OBJECT = {
    'key': 'key val',
    'anotherKey': 'anotherKey val',
};
type MY_OBJECT_KEYS = keyof typeof MY_OBJECT // "key" | "anotherKey"
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357