'Getting types of another types properties in TypeScript?

Given this type:

type Foo = {
   Prop1: Bar1,
   Prop2: Bar2
}

From this type I want to extract a union type equivalent to:

type NewType = Bar1 | Bar2;

Can this be done in TypeScript?



Solution 1:[1]

Use a combination of Keyof Type Operator and Indexed Access Types

type NewType = Foo[keyof Foo];

Playground link

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 Lesiak