【问题标题】:How can I transform a Discriminate Union into a DU where each Case is an Array of the Initial DU cases如何将区分联合转换为 DU,其中每个案例都是初始 DU 案例的数组
【发布时间】:2020-11-16 00:29:34
【问题描述】:

是否可以使用 mappend/条件类型来转换这个 DU

type MyDU =
| {kind: 'foo'}
| {kind: 'bar'}

type Transformed = DU2Array<MyDU>

这样我们得到以下结果

type Transformed =
| {kind: 'foo'}[]
| {kind: 'bar'}[]

谢谢

【问题讨论】:

    标签: typescript


    【解决方案1】:

    嗯。这比我想象的要容易

    type DU2Array<T> = T extends {} ? T[] : never
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 2017-05-06
      相关资源
      最近更新 更多