通过类型键使对象成为联合类型

时间:2019-10-11 10:04:55

标签: typescript

如何从DESIRED_RESULT开始创建类型INITIAL

来自:

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

收件人:

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

1 个答案:

答案 0 :(得分:2)

utility-typesUnionize可以做到:

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

type DESIRED_RESULT = Unionize<INITIAL>