Skip to content
#239 chunk-tuple · par 4

    ↑↓ move · ⏎ open · esc close

    No. 239 · August 22, 2026 · Hard

    Chunk

    Implement `Chunk<T, N>` so it splits a tuple into groups of `N`. A trailing group smaller than `N` is kept as-is.

    01

    Try the puzzle yourself

    Par 4

    Puzzle

    chunk-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
    • Chunk<[1, 2, 3], 2>
      [[1, 2], [3]]
    • Chunk<[1, 2, 3, 4], 2>
      [[1, 2], [3, 4]]
    • Chunk<[], 3>
      []
    • Chunk<[1, 2], 5>
      [[1, 2]]

    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 August 23, 2026

    The solution

    type Chunk<
      T extends readonly unknown[],
      N extends number,
      Acc extends unknown[] = [],
    > = Acc['length'] extends N
      ? [Acc, ...Chunk<T, N>]
      : T extends readonly [infer H, ...infer R]
        ? Chunk<R, N, [...Acc, H]>
        : Acc extends []
          ? []
          : [Acc]

    The common wrong answer

    type Chunk<
      T extends readonly unknown[],
      N extends number,
      Acc extends unknown[] = [],
    > = Acc['length'] extends N
      ? [Acc, ...Chunk<T, N>]
      : T extends readonly [infer H, ...infer R]
        ? Chunk<R, N, [...Acc, H]>
        : [Acc]

    Almost right, and wrong only where it is hardest to notice. Without the `Acc extends []` guard the empty input produces `[[]]` — a tuple containing one empty group — instead of `[]`. A perfectly divisible input hits the same path and gains a trailing empty group.

    Line by line

    1. Acc['length'] extends N

      The full-group test comes first, before the input is examined. Reaching `N` emits the group and restarts with the default empty accumulator, because `Chunk<T, N>` omits the third argument.

    2. Acc extends [] ? [] : [Acc]

      The input ran out. Either the last group is empty — meaning the previous one closed exactly on the boundary, and there is nothing left to emit — or it holds a short final group that belongs in the result.

    Takeaway

    Recursions with two exits need both exits tested. The boundary case where the input divides evenly is the one that slips through, and it is exactly the case a hand-written example usually avoids.

    Uses