Skip to content
webtype.org#216 type-level-add · par 5

No. 216 · July 24, 2026 · Brutal

Type-level Add

Implement `Add<A, B>` so it returns the sum as a number literal type. TypeScript has no type-level arithmetic, so you have to count — `BuildTuple<N>` is in scope and gives you a tuple of length `N`.

01

Try the puzzle yourself

Par 5

Puzzle

type-level-add.ts
type BuildTuple<N extends number, Acc extends unknown[] = []> =
  Acc['length'] extends N ? Acc : BuildTuple<N, [...Acc, unknown]>
Stroke 1 of 5Not run yet

Replace ??? — your solution is checked against the cases below.

Checks

4
  • Add<1, 2>
    3
  • Add<0, 0>
    0
  • Add<12, 5>
    17
  • Add<40, 2>
    42

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

How everyone did

Nobody is counting yet. Score distribution and the short game board arrive with accounts — until then your results stay on this device.

Archive
02

Annotated solution

Published July 25, 2026

The solution

type Add<A extends number, B extends number> = [
  ...BuildTuple<A>,
  ...BuildTuple<B>,
]['length']

The common wrong answer

type Add<A extends number, B extends number> =
  BuildTuple<A>['length'] extends infer X ? X : never

This reads a length back out but never combines the two tuples, so `B` is ignored entirely. The arithmetic has to happen in the tuple, not after it.

Line by line

  1. [...BuildTuple<A>, ...BuildTuple<B>]

    Variadic tuple spreads. Two tuples of length `A` and `B` concatenate into one of length `A + B` — that concatenation is the addition.

  2. ['length']

    An indexed access on a tuple type. For a fixed-length tuple the `length` property is a numeric literal type, which is exactly the answer.

Takeaway

Type-level arithmetic is counting in disguise. Tuple length is the only counter TypeScript gives you, and recursion depth caps it near a thousand — which is why nobody ships a type-level calculator.