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

Set type as an array of any property names from an interface

I have the following interface

interface Details {
  id: string;
  groups: Group[];
  name: string;
  total: number;
  open: boolean;
  debug: boolean;
  demo: boolean;
  registered: boolean;
}

I then want a type to be an array of any of the property names from the Details type.

For example ['total', 'open', 'demo'] would be valid.

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

I tried

type RequiredFields = { [key in Details]?: string };

But I get the following TypeScript error.

Type 'Details' is not assignable to type 'string | number | symbol'.
Type 'Details' is not assignable to type 'symbol'.

Any help would be greatly appreciated.

>Solution :

what you are looking for is probably:

type RequiredFields = (keyof Details)[];
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