Skip to content
webtype.org#232 zip-tuples · par 3

No. 232 · August 9, 2026 · Moderate

Zip

Implement `Zip<A, B>` so it pairs elements of two tuples positionally. When the tuples differ in length, stop at the shorter one.

01

Try the puzzle yourself

Par 3

Puzzle

zip-tuples.ts
Stroke 1 of 3Not run yet

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

Checks

4
  • Zip<[1, 2], ['a', 'b']>
    [[1, 'a'], [2, 'b']]
  • Zip<[], []>
    []
  • Zip<[1, 2, 3], ['a']>
    [[1, 'a']]
  • Zip<[1], ['a', 'b', 'c']>
    [[1, 'a']]

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 10, 2026

The solution

type Zip<A extends readonly unknown[], B extends readonly unknown[]> =
  A extends readonly [infer AH, ...infer AR]
    ? B extends readonly [infer BH, ...infer BR]
      ? [[AH, BH], ...Zip<AR, BR>]
      : []
    : []

The common wrong answer

type Zip<A extends readonly unknown[], B extends readonly unknown[]> =
  A extends readonly [infer AH, ...infer AR]
    ? B extends readonly [infer BH, ...infer BR]
      ? [AH, BH, ...Zip<AR, BR>]
      : []
    : []

Without the inner brackets the pairs are spread flat into the result: `[1, "a", 2, "b"]` instead of `[[1, "a"], [2, "b"]]`. Zipping is about structure, and the structure lives in those brackets.

Line by line

  1. [[AH, BH], ...Zip<AR, BR>]

    The inner `[AH, BH]` is one pair; the spread appends the pairs from the rest. Nesting a tuple inside a tuple is how you keep the pairing visible in the type.

  2. : []

    Both fall-through branches end the recursion, so whichever tuple runs out first stops the zip. That gives the shorter-of-the-two behaviour without measuring either length.

Takeaway

Walking two structures at once is just two destructuring conditionals nested. The interesting design decision is what happens when they disagree in length — and here, doing nothing special is the answer.