Typescript类型体操 - Tuple To Union

题目

中文

实现泛型TupleToUnion<T>,它返回元组所有值的合集。

例如

type Arr = ['1', '2', '3']

type Test = TupleToUnion<Arr> // expected to be '1' | '2' | '3'

English

Implement a generic TupleToUnion<T> which covers the values of a tuple to its values union.

For example

type Arr = ['1', '2', '3']

type Test = TupleToUnion<Arr> // expected to be '1' | '2' | '3'

答案

type TupleToUnion<T extends unknown[]> = T[number]

在线演示

posted @ 2022-09-04 20:35  Laggage  阅读(66)  评论(0编辑  收藏  举报