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.

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)[];

Leave a Reply