[SOLVED] Getting types of another types properties in TypeScript?

Issue

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

Use a combination of Keyof Type Operator and Indexed Access Types

type NewType = Foo[keyof Foo];

Playground link

Answered By – Lesiak

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *