【问题标题】:Concatenate two TypeScript type as one将两个 TypeScript 类型连接为一个
【发布时间】:2022-01-09 20:14:11
【问题描述】:

我想知道是否有一种方法可以连接两种打字稿类型作为一个例子:

type Size = 'xl' | 'xxl' ...;
let textSize: 'text-' & Size;
// So my type is something like : 'text-xl' | 'text-xxl'

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    通过使用template literal type

    type Size = 'xl' | 'xxl';
    type TextSizeType = `text-${Size}`;
    

    TS 将TextSizeType 解析为

    type TextSizeType = "text-xl" | "text-xxl"
    

    这听起来像你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 2020-08-10
      • 2018-10-22
      • 2016-10-03
      • 2023-03-06
      相关资源
      最近更新 更多