Type-level programming
12 steps in 5 sets of TypeScript.
Conditional types, mapped types, template literal types, and recursion. This is programming, executed by the compiler, with types as the values.
Twelve steps, ending with exhaustiveness checks and type-level tests. Genuinely useful in a library and genuinely possible to overdo in an application. Both are worth knowing by hand.
Conditional types
- Conditional types and inferTypes that branch, and extract inner types.
- Conditional types and inferA type-level if, and the keyword that pulls a type out of a position.
- Distributive conditionalsA conditional over a naked type parameter runs once per union member.
Mapped types
- Mapped typesBuilding a new type by walking another's keys.
- Mapped types with key remappingBuilding a type from another one, renaming the keys as you go.
- Adding and removing modifiersA mapped type can add or strip readonly and optional.
Strings & recursion
- Template literal typesString types built from other string types, at compile time.
- Recursive typesA type that refers to itself, which is how JSON and trees are typed.
Exhaustiveness & tests
- Discriminated unionsA shared literal field that makes a union exhaustive.
- ExhaustivenessThe compiler proves you handled every case, if you let it.
- Testing typesAssertions that run in the compiler, not at run time.
Encore
- typed-router.tsA router whose path parameters are extracted from the pattern at compile time.