0Pricing
TypeScript Academy · Lesson

Counting with Tuple Length

Represent numbers as tuple lengths in types.

Counting with Tuple Length is a free TypeScript Academy lesson on CoddyKit — lesson 1 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.

Numbers Need a Representation

The type system cannot add numeric literals directly. The classic workaround is to represent a number N as a tuple with N elements. The contents do not matter; only the length does.

type Three = [unknown, unknown, unknown];
type N = Three["length"]; // 3

Reading Length

Every tuple type has a length property that is a numeric literal type. Indexing with ["length"] reads it back as a number you can use.

type A = [1, 2, 3, 4]["length"]; // 4
type B = []["length"];           // 0
type C = ["x"]["length"];        // 1

Why unknown Filler

We use unknown as the element type because the values are irrelevant, only the count matters. Any type works, but unknown signals "placeholder" clearly.

type Two = [unknown, unknown];
type N = Two["length"]; // 2

Building a Tuple of Length N

To go the other way, build a tuple of a target length using recursion. Add elements until the length matches N, then return the tuple. This is the famous BuildTuple helper.

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

type A = BuildTuple<3>; // [unknown, unknown, unknown]

How BuildTuple Loops

Trace BuildTuple<3>:

  • Acc length 0, not 3, add one
  • Acc length 1, not 3, add one
  • Acc length 2, not 3, add one
  • Acc length 3, equals 3, return

The accumulator length is the loop counter.

type A = BuildTuple<2>; // [unknown, unknown]

Length Is the Bridge

Two operations let you move between numbers and tuples:

  • Number to tuple: BuildTuple<N>
  • Tuple to number: T["length"]

All type arithmetic builds on crossing this bridge.

type FromN = BuildTuple<4>;        // tuple of length 4
type BackToN = FromN["length"];    // 4

Comparing by Length

Because tuple length is a literal, you can check equality of counts. Build tuples and compare their lengths via assignability.

type SameLength<A extends unknown[], B extends unknown[]> =
  A["length"] extends B["length"] ? true : false;

type X = SameLength<[1, 2], ["a", "b"]>;   // true
type Y = SameLength<[1], ["a", "b"]>;      // false

A Generic Length Counter

You can re-implement length via recursion to understand it, though reading ["length"] is faster. This shows counting is just walking a tuple.

type Len<T extends unknown[], Acc extends unknown[] = []> =
  T extends [unknown, ...infer R]
    ? Len<R, [...Acc, unknown]>
    : Acc["length"];

type A = Len<["a", "b", "c"]>; // 3

Tuples as Counters

Think of the tuple as a tally: each element is one mark. Adding an element increments; removing one decrements. This mental model makes arithmetic intuitive.

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

type A = Inc<3>; // 4

Decrement Preview

Decrementing builds the tuple for N, then infers everything except the last element and reads the shorter length. You will formalize this in the next lesson.

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

type A = Dec<3>; // 2
type B = Dec<0>; // 0

Limits to Remember

This technique works for small non-negative integers. Very large N hits recursion limits, and there is no built-in for negatives or decimals. Within those bounds it is reliable and fully static.

type A = BuildTuple<5>["length"]; // 5 (fine)
// BuildTuple<10000> would error: too deep

Quick Check

Test your understanding of tuple-length counting.

Recap

Counting is the basis of type-level math.

  • Represent N as a tuple of length N.
  • Read the number with T["length"].
  • BuildTuple<N> goes from number to tuple.
  • Works for small non-negative integers only.

Next: real addition and subtraction.

Frequently asked questions

Is the “Counting with Tuple Length” lesson free?

Yes — the full text of “Counting with Tuple Length” 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 “Counting with Tuple Length”?

Represent numbers as tuple lengths in types. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Counting with Tuple Length” 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