Sobes.tech
Middle+

How is your experience with TypeScript? There is a Foo type with fields a: string, b: number, c: null, d: boolean. How to create a FooKeys type based on Foo that includes all keys?

sobes.tech AI

Answer from AI

In TypeScript, to obtain a type that contains all the keys of type Foo, you can use the keyof operator.

If there is a type:

type Foo = {
  a: string;
  b: number;
  c: null;
  d: boolean;
};

Then the type with keys will be:

type FooKeys = keyof Foo; // 'a' | 'b' | 'c' | 'd'

FooKeys is a union of string literals corresponding to the field names of type Foo.

How is your experience with TypeScript? There is a… - sobes.tech