Skip to main content

Tuple to Union

introduction#

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

For example

ts
type Arr = ['1', '2', '3']
const a: TupleToUnion<Arr> // expected to be '1' | '2' | '3'
ts
type Arr = ['1', '2', '3']
const a: TupleToUnion<Arr> // expected to be '1' | '2' | '3'
View on GitHub

start point#

ts
/* _____________ Your Code Here _____________ */
 
type TupleToUnion<T> = any
 
/* _____________ Test Cases _____________ */
type cases = [
Expect<Equal<TupleToUnion<[123, '456', true]>, 123 | '456' | true>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TupleToUnion<[123]>, 123>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
Try
ts
/* _____________ Your Code Here _____________ */
 
type TupleToUnion<T> = any
 
/* _____________ Test Cases _____________ */
type cases = [
Expect<Equal<TupleToUnion<[123, '456', true]>, 123 | '456' | true>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<TupleToUnion<[123]>, 123>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
Try
take the challenge

my solutions#

Spoiler warning // Click to reveal answer
ts
type TupleToUnion<T extends any[]> = T[number]
Try
ts
type TupleToUnion<T extends any[]> = T[number]
Try
view more solutions