Type-Level Programming
Types are a language
Section titled “Types are a language”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.
What this module covers
Section titled “What this module covers”| Lesson | What you’ll learn |
|---|---|
| Mapped types | Iterate over a type’s keys to transform it — build Partial and Readonly yourself |
| keyof & indexed access | keyof T and T[K] — the lookups that power everything else |
| Template literal types | Compute string types, and pattern-match strings with infer |
| The utility types | The built-ins, and how they are actually implemented |
| Recursive types | Types that call themselves — and where the compiler draws the line |
The building blocks fit together
Section titled “The building blocks fit together”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)"]
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.