Home > Software engineering >  Getting types of another types properties in TypeScript?
Getting types of another types properties in TypeScript?

Time:03-21

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?

CodePudding user response:

Use a combination of Keyof Type Operator and Indexed Access Types

type NewType = Foo[keyof Foo];

Playground link

  • Related