0Pricing
TypeScript Academy · Lesson

Type-Level Addition and Subtraction

Build arithmetic operations from tuple manipulation.

Type-Level Addition and Subtraction is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Arithmetic by Length

With the tuple-length representation, addition becomes concatenation and subtraction becomes removing a prefix. You compute on tuples, then read the resulting length.

type BuildTuple<N extends number, Acc extends unknown[] = []> =
  Acc["length"] extends N ? Acc : BuildTuple<N, [...Acc, unknown]>;

Addition

To add A + B: build a tuple of length A, build one of length B, spread both into a new tuple, and read its length.

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

type S = Add<3, 4>; // 7

Why Concatenation Adds

A tuple of length 3 followed by a tuple of length 4 yields a tuple of length 7. Joining the marks of two tallies gives a combined tally, so the lengths add.

type A = Add<2, 2>; // 4
type B = Add<0, 5>; // 5
type C = Add<1, 6>; // 7

Subtraction by Prefix

To compute A - B: build a tuple of length A, then infer it as a B-length prefix followed by a rest. The rest has length A - B.

type Sub<A extends number, B extends number> =
  BuildTuple<A> extends [...BuildTuple<B>, ...infer Rest]
    ? Rest["length"]
    : never;

type D = Sub<7, 4>; // 3

Walking Subtraction

For Sub<7, 4>: build length-7, match a length-4 prefix, capture Rest (length 3), and read its length. The prefix removed is exactly B marks.

type A = Sub<5, 2>; // 3
type B = Sub<4, 4>; // 0

Underflow Returns never

Type-level numbers here are non-negative. If B > A, no valid prefix exists, the pattern fails, and the result is never. You can default it to 0 if you prefer.

type A = Sub<2, 5>; // never

type SubSafe<A extends number, B extends number> =
  [Sub<A, B>] extends [never] ? 0 : Sub<A, B>;
type B = SubSafe<2, 5>; // 0

Increment and Decrement

Special cases of add and subtract by one are common enough to name. Inc appends one element; Dec removes one.

type Inc<N extends number> = [...BuildTuple<N>, unknown]["length"];
type Dec<N extends number> =
  BuildTuple<N> extends [unknown, ...infer R] ? R["length"] : 0;

type A = Inc<4>; // 5
type B = Dec<4>; // 3

Multiplication by Repetition

Multiplication is repeated addition. Add B to an accumulator A times by counting down with Dec and accumulating with Add.

type Mul<A extends number, B extends number, Acc extends number = 0> =
  A extends 0 ? Acc : Mul<Dec<A>, B, Add<Acc, B>>;

type A = Mul<3, 4>; // 12

The BuildTuple Helper

Every operation here depends on BuildTuple. It is the single reusable primitive: give it a number, get a tuple of that length. Keep it in one place and build the rest on top.

type Five = BuildTuple<5>; // [unknown, unknown, unknown, unknown, unknown]
type N = Five["length"];   // 5

Composing Operations

Because each operation takes and returns a number, they compose freely. You can write expressions like Add<Mul<2, 3>, 4> and the compiler evaluates them.

type Result = Add<Mul<2, 3>, 4>; // 10

Practical Caution

These tricks are great for small counts: array indices, fixed sizes, bounded ranges. For large numbers or general math, the recursion limit and lack of negatives make them impractical. Use them where the values stay small and static.

type A = Add<8, 7>;  // 15 (fine)
// Add<5000, 5000> would exceed recursion limits

Quick Check

Test your understanding of type-level arithmetic.

Recap

You implemented arithmetic in the type system.

  • Add: concatenate tuples, read length.
  • Sub: infer a prefix, read the rest length.
  • Inc/Dec are the one-step versions.
  • BuildTuple is the shared primitive.

Next: comparing numbers at the type level.

Frequently asked questions

Is the “Type-Level Addition and Subtraction” lesson free?

Yes — the full text of “Type-Level Addition and Subtraction” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Type-Level Addition and Subtraction”?

Build arithmetic operations from tuple manipulation. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Type-Level Addition and Subtraction” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Counting with Tuple Length
  2. Type-Level Addition and Subtraction
  3. Type-Level Comparisons
  4. Practical Numeric Type Utilities
← Back to TypeScript Academy