将两个TypeScript类型连接为一个



我想知道是否有一种方法可以将两种类型脚本类型连接起来,例如:

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

通过使用模板文字类型。

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

TS将TextSizeType解析为

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

这听起来像你想要的。

最新更新