[Typescript challenge] 19. Medium - Tuple to Union

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'

 

/* _____________ Your Code Here _____________ */

type TupleToUnion<T> = T extends [infer First, ...(infer RT)] ? First | TupleToUnion<RT>: never


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<TupleToUnion<[123, '456', true]>, 123 | '456' | true>>,
  Expect<Equal<TupleToUnion<[123]>, 123>>,
]

 

posted @ 2022-09-08 13:44  Zhentiw  阅读(37)  评论(0编辑  收藏  举报