TypeScript Cookbook: Real World Type-Level Programming
Best for: Experienced TypeScript developers writing library- or framework-level generic types who need the type system to do real work, not just annotate values.
- ISBN-13: 9781098136659
- Level: advanced
- Published: 2023
Stefan Baumgartner writes extensively about TypeScript’s type system on
fettblog.eu, and this cookbook is the deep-dive companion to that: roughly
forty recipes, each solving a specific real problem — typing a generic
fetch wrapper so the response shape depends on the request, building a
strongly-typed event emitter, validating a JSON shape at the type level —
using conditional types, mapped types, template literal types, and
recursive tuple types.
Its strength is that every recipe explains the inference mechanics behind the solution, not just the code that happens to work. A reader comes away understanding why a given conditional type distributes over a union, not just that copying the pattern fixes the error — which is what makes the recipes generalizable to a reader’s own problems instead of only solving the exact one printed in the book.
It is squarely an advanced book, and that’s also its main limitation: most recipes assume real comfort with conditional and mapped types already, so it’s a poor first or even second TypeScript book. A few of the more exotic variadic-tuple and recursive-type patterns are also the kind of thing that makes code genuinely hard for a teammate to read later, and the book is better at showing what’s possible than at cautioning when a simpler, less clever type would serve the team better.
Compared to Effective TypeScript: Effective TypeScript’s later chapters gesture at type-level programming; this book is the entire text on that one topic, for when a project’s types have already outgrown what Effective TypeScript’s advice covers.