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

时间:2020-12-30 07:55:06

标签: typescript tuples

我有一个类型

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

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

但输出还是一样的'tipe-1' | 'tipe-2' | 'tipe-3'

1 个答案:

答案 0 :(得分:3)

您可以使用 Template Literal Types

<listener>
        <listener-class>com.config.ServletStartListener</listener-class>
</listener>

这等同于:

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

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

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