向每个元组添加一个字符串



我有一个类型

type mytype = 'tipe-1' | 'tipe-2' | 'tipe-3'

如何这样打字type mytype2 = '1' | '2' | '3'

但是输出仍然是相同的'tipe-1' | 'tipe-2' | 'tipe-3'

您可以为此使用模板文字类型:

type MyType = `tipe-${1 | 2 | 3}`

这与相同

type MyType = 'tipe-1' | 'tipe-2' | 'tipe-3'

如果你想在多个地方使用它,你可以把它抽象出来:

type MakeMyType<T extends number> = `tipe-${T}`;
type MyType = MakeMyType<1 | 2 | 3>;

相关内容

最新更新