Skip to content
webtype.orgwebtype.org#250 fill-tuple · par 3

    ↑↓ move · ⏎ open · esc close

    No. 250 · September 2, 2026 · Moderate

    Fill

    Implement `Fill<N, V>` so it produces a tuple of exactly `N` elements, each of type `V`.

    01

    Try the puzzle yourself

    Par 3

    Puzzle

    fill-tuple.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

    3
    • Fill<3, 'x'>
      ['x', 'x', 'x']
    • Fill<0, number>
      []
    • Fill<2, true>
      [true, true]

    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 September 3, 2026

    The solution

    type Fill<N extends number, V, R extends V[] = []> = R['length'] extends N ? R : Fill<N, V, [...R, V]>

    The common wrong answer

    type Fill<N extends number, V, R extends V[] = []> = R['length'] extends N ? R : [...R, V]

    This adds one element and then stops. The `else` branch has to call `Fill` again — appending is the step, not the answer. Written this way, `Fill<3, "x">` returns `["x"]`, because the type ran exactly once.

    Line by line

    1. R['length'] extends N ? R : ...

      The accumulator is also the counter. A tuple knows its own length, so there is no arithmetic to do — just ask whether it has reached `N` yet.

    2. Fill<N, V, [...R, V]>

      Each recursion hands the next call a slightly longer tuple. `Fill<0, number>` never enters this branch at all, because an empty accumulator already has the requested length.

    Takeaway

    An accumulator parameter turns a recursive type into a loop with a variable. The base case is a question about the accumulator, and the step is the same type called with a bigger one.

    Uses