我有一个类型
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>;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句