【问题标题】:Make union type of objects from type keys从类型键创建对象的联合类型
【发布时间】:2020-02-08 20:15:25
【问题描述】:

如何从INITIAL 开始创建类型DESIRED_RESULT

来自:

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

到:

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

【问题讨论】:

    标签: typescript


    【解决方案1】:

    utility-typesUnionize 正是这样做的:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-06
    • 2021-11-26
    • 2022-01-03
    • 2020-05-21
    • 1970-01-01
    • 2021-12-02
    • 2019-10-20
    • 2020-03-04
    相关资源
    最近更新 更多