0Pricing
TypeScript Academy · Lesson

Practical Numeric Type Utilities

Apply type arithmetic to range and length constraints.

Practical Numeric Type Utilities is a free TypeScript Academy lesson on CoddyKit — lesson 4 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.

From Theory to Tools

Arithmetic and comparison become useful when packaged as practical utilities: ranges, length-enforced arrays, clamped indices. This lesson turns the primitives into things you would actually ship.

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

Enumerate: 0 to N-1

A foundational helper produces a union of all numbers from 0 to N-1. Build a tuple of length N and read every key index.

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

type A = Enumerate<4>; // 0 | 1 | 2 | 3

Range Types

A Range<Start, End> yields the numbers from Start up to End - 1. Take Enumerate<End> and exclude the values below Start.

type Range<S extends number, E extends number> =
  Exclude<Enumerate<E>, Enumerate<S>>;

type A = Range<2, 6>; // 2 | 3 | 4 | 5

Constraining a Parameter

Use a range to restrict valid arguments. A function accepting only a small dice roll can take Range<1, 7> so the compiler rejects out-of-bounds values.

type DiceFace = Range<1, 7>; // 1|2|3|4|5|6
declare function roll(face: DiceFace): void;
roll(4); // ok
// roll(8); // compile error

Enforcing Array Length

A fixed-length array type rejects arrays of the wrong size. Build a tuple of N elements of type T using recursion.

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

type RGB = FixedArray<number, 3>; // [number, number, number]

Using Fixed Length

Now misuse is a compile error. A point in 3D must have exactly three coordinates.

type Vec3 = FixedArray<number, 3>;
const v: Vec3 = [1, 2, 3]; // ok
// const w: Vec3 = [1, 2]; // error: missing element

Clamping Indices

You can clamp an index type to valid array positions. Combine a tuple length with Enumerate so only in-bounds indices are accepted.

type Indices<T extends readonly unknown[]> = Enumerate<T["length"]>;

type Arr = readonly ["a", "b", "c"];
type I = Indices<Arr>; // 0 | 1 | 2

Safe Indexed Access

With a valid-index type you can write a getter whose index parameter cannot go out of bounds, catching off-by-one errors at compile time.

declare function at<T extends readonly unknown[]>(
  arr: T,
  i: Indices<T>
): T[Indices<T>];

const x = at(["a", "b", "c"] as const, 2); // ok
// at(["a", "b", "c"] as const, 3); // error

Min and Max Helpers

Layer comparison on top to pick the larger or smaller of two numbers, useful for clamping a value into a range.

type Max<A extends number, B extends number> =
  GreaterThan<A, B> extends true ? A : B;
type Min<A extends number, B extends number> =
  GreaterThan<A, B> extends true ? B : A;

type X = Max<3, 8>; // 8
type Y = Min<3, 8>; // 3

Composing Utilities

These utilities combine. A tuple of length within a range, an index clamped to its array, a value bounded by min and max: each is a small, reusable type-level function.

type ValidPort = Range<1, 4>; // 1 | 2 | 3 (toy example)
type Slots = FixedArray<ValidPort, 2>; // [1|2|3, 1|2|3]

When It Is Worth It

Type arithmetic shines for small, fixed bounds: RGB tuples, dice faces, board coordinates, protocol field sizes. It is not worth it for large or dynamic numbers, where recursion limits and complexity outweigh the safety. Reach for it when the bound is small and the guarantee is valuable.

type Board = FixedArray<FixedArray<0 | 1, 3>, 3>; // 3x3 grid

Quick Check

Test your understanding of practical numeric utilities.

Recap

You built a small library of numeric type utilities.

  • Enumerate and Range produce number unions.
  • FixedArray enforces exact lengths.
  • Indices clamps access to valid positions.
  • Min/Max bound values.

Use them for small fixed bounds. Course 23 next: simulating higher-kinded types.

Frequently asked questions

Is the “Practical Numeric Type Utilities” lesson free?

Yes — the full text of “Practical Numeric Type Utilities” 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 “Practical Numeric Type Utilities”?

Apply type arithmetic to range and length constraints. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Practical Numeric Type Utilities” 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