将两个 TypeScript 类型连接为一个

Jul*_*leZ 2 javascript typescript

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

type Size = 'xl' | 'xxl' ...;
let textSize: 'text-' & Size;
// So my type is something like : 'text-xl' | 'text-xxl'
Run Code Online (Sandbox Code Playgroud)

AKX*_*AKX 5

通过使用模板文字类型

type Size = 'xl' | 'xxl';
type TextSizeType = `text-${Size}`;
Run Code Online (Sandbox Code Playgroud)

TSTextSizeType决定

type TextSizeType = "text-xl" | "text-xxl"
Run Code Online (Sandbox Code Playgroud)

这听起来像你想要的。