Skip to content
webtype.org#234 tuple-to-union · par 2

No. 234 · August 17, 2026 · Gentle

TupleToUnion

Implement `TupleToUnion<T>` so it turns a tuple into the union of its element types. The whole thing is one indexed access.

01

Try the puzzle yourself

Par 2

Puzzle

tuple-to-union.ts
Stroke 1 of 2Not run yet

Replace ??? — your solution is checked against the cases below. Tab indents; press Escape then Tab to move focus out.

Checks

3
  • TupleToUnion<[1, 2, 3]>
    1 | 2 | 3
  • TupleToUnion<['a']>
    'a'
  • TupleToUnion<[]>
    never

How a check is judged Exact type equality, not assignability — an intersection is not the same as the flattened object.

How everyone did

Fewer than 5 people have solved this one so far. The distribution appears once there is enough of a sample to mean anything.

Archive
02

Annotated solution

Published August 18, 2026

The solution

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

The common wrong answer

type TupleToUnion<T extends readonly unknown[]> =
  T extends readonly [infer H, ...infer R] ? H : never

A reasonable instinct — destructure and take the head — but it stops there and returns only the first element. Recursing and unioning the results would work, and it is exactly the long way round: `T[number]` already does it.

Line by line

  1. T[number]

    An indexed access whose key is the whole `number` type. Since every valid numeric index selects one element, asking for all of them at once yields the union of every element type.

  2. TupleToUnion<[]>

    The empty tuple has no valid numeric index, so the union of zero things comes back — and the union of nothing is `never`.

Takeaway

Reach for recursion only when indexing cannot express the question. `T[number]`, `keyof T` and `T[keyof T]` answer a surprising share of tuple and object problems in a single line.