Annotated solution
Published August 1, 2026The solution
type Subtract<A extends number, B extends number> = BuildTuple<A> extends [...BuildTuple<B>, ...infer Rest] ? Rest['length'] : never
The common wrong answer
type Subtract<A extends number, B extends number> = [...BuildTuple<A>, ...BuildTuple<B>]['length']
That is addition wearing a different name. Concatenating the two tuples can only ever make them longer; subtraction has to take a tuple *apart*, which means pattern-matching rather than building.
Line by line
[...BuildTuple<B>, ...infer Rest]The pattern hard-codes a prefix of exactly `B` elements and lets `Rest` absorb whatever is left. Matching succeeds only when the subject is at least that long.
: neverWhen `B > A` there is no way to line up the prefix, the conditional fails, and `never` falls out — which is the honest answer for a subtraction this arithmetic cannot represent.
Takeaway
Building tuples adds; destructuring them subtracts. Every arithmetic operation TypeScript can do at the type level is one of those two motions in disguise.