Home > database >  Access dynamic property of an interface in Typescript
Access dynamic property of an interface in Typescript

Time:09-01

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.

CodePudding user response:

type X = Person[keyof Person]; 

Will return type:

type X = {
  name: string;
  alias: string;
  active: string[];
  values: string[];
}
  • Related