TS 将类型定义为等于类型化数组的值之一
TS define type to equal one of values of typed array
假设我可以访问这样定义的类型 Keys
:
type Keys = ('AA' | 'BB' | 'CC')[]
- 如何使用类型
Keys
创建一个新类型 Key
,以便这个新类型等于:
type Key = 'AA' | 'BB' | 'CC'
请注意,我无法逐字指定允许的值(AA
、BB
等),我只有 Keys
类型可用。
您可以使用 indexed type query:
type Keys = ('AA' | 'BB' | 'CC')[]
type Key = Keys[number]
假设我可以访问这样定义的类型 Keys
:
type Keys = ('AA' | 'BB' | 'CC')[]
- 如何使用类型
Keys
创建一个新类型Key
,以便这个新类型等于:
type Key = 'AA' | 'BB' | 'CC'
请注意,我无法逐字指定允许的值(AA
、BB
等),我只有 Keys
类型可用。
您可以使用 indexed type query:
type Keys = ('AA' | 'BB' | 'CC')[]
type Key = Keys[number]