'Get union type from indexed object values

Say I have an indexed type:

type X = {
 a: 'A',
 b: 'B'
}

is it possible to get (derived) from it:

type V = 'A' | 'B'

not using explicit method like:

type V = X['a'] | X['b']

What I want is something like keyof (for getting keys union type), but for values.



Solution 1:[1]

I realize this has already been answered but if you are here from google and you are looking for a way to also convert an objects values to a union (as the title of the question suggests), you can do this:

const X = {
 a: 'A',
 b: 'B'
 // mark properties as readonly, otherwise string type inferred 
} as const

type XValues = typeof X[keyof typeof X]

// "A" | "B

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1