Skip to content
webtype.orgwebtype.org#222 reverse-tuple · par 4

    ↑↓ move · ⏎ open · esc close

    No. 222 · July 30, 2026 · Hard

    Reverse

    Implement `Reverse<T>` so it returns the tuple with its elements in the opposite order.

    01

    Try the puzzle yourself

    Par 4

    Puzzle

    reverse-tuple.ts
    Stroke 1 of 4Not run yet

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

    Checks

    4
    • Reverse<[1, 2, 3]>
      [3, 2, 1]
    • Reverse<[]>
      []
    • Reverse<['a']>
      ['a']
    • Reverse<[1, 'b', true]>
      [true, 'b', 1]

    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.

    Short game

    Fewest characters

    No public scores yet.

    Archive
    02

    Annotated solution

    Published July 31, 2026

    The solution

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

    The common wrong answer

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

    This recurses correctly and rebuilds the tuple in the order it took it apart, so it is an elaborate identity function. Reversal is entirely a question of which side of the spread the head goes on.

    Line by line

    1. [infer H, ...infer R]

      The classic head/tail split. `H` binds the first element, `R` binds a tuple of everything after it.

    2. [...Reverse<R>, H]

      The head is appended *after* the reversed tail. The first element of the input therefore ends up last, and every level of the recursion pushes its head one place further back.

    Takeaway

    Head-and-tail recursion rebuilds a tuple one element at a time; the order you reassemble in is the only thing that distinguishes a copy from a reversal, a filter or a map.

    Uses