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

问题描述

假设我可以访问这样定义的类型Keys

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

请注意,我无法从字面上指定允许的值(AABB等),我只能使用Keys类型。

解决方法

您可以使用indexed type query

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

Playground Link