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 extendsnumber, Acc extendsunknown[] = []> =
Acc['length'] extends N ? Acc : BuildTuple<N, [...Acc, unknown]>
type Add<A extendsnumber, B extendsnumber> = ???
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.
type Add<A extendsnumber, B extendsnumber> = [
...BuildTuple<A>,
...BuildTuple<B>,
]['length']
The common wrong answer
type Add<A extendsnumber, B extendsnumber> =
BuildTuple<A>['length'] extendsinfer 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
[...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.
['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.