TS将类型定义为等于类型化数组的值之一



假设我可以访问Keys类型,该类型定义如下:

type Keys = ('AA' | 'BB' | 'CC')[]
  1. 如何使用类型Keys创建新类型Key,使此新类型等于:
type Key = 'AA' | 'BB' | 'CC'

请注意,我不能直接指定允许的值(AABB等(,我只有Keys类型可供使用。

您可以使用索引类型查询:

type Keys = ('AA' | 'BB' | 'CC')[]
type Key = Keys[number]

游乐场链接

最新更新