TupleToUnion
题目
Github: Tuple to union
实现泛型TupleToUnion<T>
,它返回元组所有值的合集。
type Arr = ['1', '2', '3']
type Test = TupleToUnion<Arr> // expected to be '1' | '2' | '3'
解题思路
知识点
- 元组
- 联合类型
- 通过索引访问元组成员
在访问元组的成员时,可以通过 [number]
索引,访问一个 由元组所有成员构成的联合类型。
Answer
type TupleToUnion<T extends any[]> = T[number]