This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.
AI solution for TypeScript puzzle on BFE.dev
18. implement TupleToUnion<T>
type TupleToUnion<T extends any[]> = T[number];
Explanation:
T
is a generic parameter that extends an array of any typeT extends any[]
.T[number]
returns a union type of all the elements of the tupleT
.
For example, given type Foo
as [string, number, boolean]
, T
will be inferred as [string, number, boolean]
, and T[number]
will be "string" | number | boolean
, which is the expected union type.