Andre Elrico
Andre Elrico

Reputation: 11480

Make union type of objects from type keys

How can I create a type DESIRED_RESULT starting from INITIAL.

from:

export type INITIAL = {
  aa: boolean,
  bb: string,
  cc: number[],
};

to:

export type DESIRED_RESULT =
{ aa: boolean } |
{ bb: string } |
{ cc: number[] };

Upvotes: 1

Views: 400

Answers (1)

georg
georg

Reputation: 214949

utility-types has Unionize that does exactly that:

export type Unionize<T extends object> = {
  [P in keyof T]: { [Q in P]: T[P] }
}[keyof T];

type DESIRED_RESULT = Unionize<INITIAL>

Upvotes: 2

Related Questions