Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use a combination of Keyof Type Operator and Indexed Access Types

type NewType = Foo[keyof Foo];

Playground link

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading