1

How can I catch all types of an interface keys?

I have below object:

interface WorkingTime {
  id?: number;
  openTime: number;
  closeTime: number;
  type?: string;
  status: 0 | 1 | 2;
  day: 1 | 2 | 3 | 4 | 5 | 6 | 7;
  clinicId?: number;
  clinicStaffId?: number;
}

Now I want set a type which that get all types of the interface and the result will be as below:

type X = string | number | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;

Thanks for your participating to answer my question

Saeed Hemmati
  • 453
  • 2
  • 11
  • Note that the type of `id` is `number | undefined` unless you have compiler options to show otherwise, so you'd get `undefined` in there also. And if you write `type X = string | number | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;` you will see that reduced to `type X = string | number`; So the [obvious way to do this](https://stackoverflow.com/q/49285864/2887218) will give you just `string | number | undefined`. What do you actually need this for? – jcalz Mar 15 '22 at 15:37

1 Answers1

1
type X = Required<WorkingTime>[keyof WorkingTime];

Each number(0, 1, 2, ...) is extended from number, so X cannot be string | number | 0 | 1 | 2 ... but should be string | number

grace
  • 270
  • 1
  • 9