0

We are currently using TS 2.3 (can't upgrade to 2.4 at the moment so using enums for this isn't an option).

We have an object type for something that looks like this

type myObject = {
  keyOn: 'valueOne';
  keyTwo: 'valueTwo';
  keyThree: 'valueThree;
}

Ideally, I would like to be able to create a type that is made up of the string literals that are the values on that object type. So something like this:

type valueLiterals = 'valueOne' | 'valueTwo' | 'valueThree'

Similar to what you would be able to produce for the keys by using

type objectKeys = keyOf myObject

Is there some version of a valueof or similar that would allow automatic generation of the valueLiterals type?

bbop99
  • 1,625
  • 1
  • 11
  • 25
  • Possible duplicate of [Is there a `valueof` similar to `keyof` in Typescript?](https://stackoverflow.com/questions/49285864/is-there-a-valueof-similar-to-keyof-in-typescript) – jcalz Oct 25 '18 at 14:17

1 Answers1

2

You can use a type query with keyof T:

type values = myObject [keyof myObject]
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357