Skip to content

Type-Level Programming

So far types have described data. In this module they start to compute over it. Given one type you can derive another: make every field optional, pick a subset of keys, extract the return type of a function, or parse a string into a union — all at the type level, evaluated by the compiler before your code runs.

This is real programming. The type system has variables (type parameters), branching (conditional types), iteration (mapped types and recursion), and functions (generic type aliases). People have implemented arithmetic and even parsers purely in types. You will not do that at work — but understanding the machinery is what lets you read library types and write the handful of clever types that genuinely pay off.

LessonWhat you’ll learn
Mapped typesIterate over a type’s keys to transform it — build Partial and Readonly yourself
keyof & indexed accesskeyof T and T[K] — the lookups that power everything else
Template literal typesCompute string types, and pattern-match strings with infer
The utility typesThe built-ins, and how they are actually implemented
Recursive typesTypes that call themselves — and where the compiler draws the line
flowchart TB
  keyof["keyof T
(get the keys)"] --> mapped["Mapped types
(iterate the keys)"]
  indexed["T[K]
(look up value types)"] --> mapped
  cond["Conditional types + infer
(branch and capture)"] --> util["Utility types
(Pick, Exclude, ReturnType)"]
  mapped --> util
  tmpl["Template literals
(compute strings)"] --> util
  util --> rec["Recursion
(repeat until done)"]
How the type-level tools compose

Almost every advanced type in the wild is a combination of five primitives: keyof to get keys, indexed access to look up value types, mapped types to iterate, conditional types with infer to branch and capture, and recursion to repeat. Learn these five and library .d.ts files stop being hieroglyphics.

What does "type-level programming" mean?
Which five primitives compose into most advanced types?
A generic type like `Box<T>` is best understood as: