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

Access dynamic property of an interface in Typescript

If I have an interface:

export interface Person {
  [key: string]: {
    name: string;
    alias: string;
    active: string[];
    values: string[];
  };
}

If I want to access:

{
  name: string;
  alias: string;
  active: string[];
  values: string[];
};

How can I do this? I tried Person[keyof typeof Person] but this does not work.

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 :

type X = Person[keyof Person]; 

Will return type:

type X = {
  name: string;
  alias: string;
  active: string[];
  values: string[];
}
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