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

how to make a type which only extracts property values from the interface in typescript?

I exactly don’t know how to express my intention in a single sentence, so I will try my best.

interface A {
  person: {
    name: string;
    age: number;
  },
  count: number
}

type B = Pick<A, 'person'>

// type B = {person: {name: string; age: number;}}

As we can see, if we use Pick utility type, type B has a key person.

What if I want to get rid of the key(person), and only want to have those property values like below type C?

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

 // type C which I want to make.
 type C = {
   name: string;
   age: number;
 }

 // something like StripKeyOut<Pick<A, 'person'>> is possible?

appreciate in advance for your help.

>Solution :

Typescript has Indexed Access Types: type B = A['person']

Playground

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