Skip to content

Typescript Generics Patterns And Constraints Textbook

A free, self-paced textbook in 7 chapters. Read a chapter, then drill it with the 241 companion flashcards using spaced repetition.

7 chapters · 241 cards · Updated

Chapters

  1. 1Foundations of Generics

    A generic in TypeScript is a placeholder for a type that is filled in at the call site, letting a single function, class, or interface operate over many types while preserving the...

  2. 2Built-in Utility Types and Modifiers

    TypeScript ships a rich set of utility types that compose new shapes from existing ones. The companions of Partial<T> are Required<T>—implemented as { [K in keyof T]-?:...

  3. 3Conditional Types and Inference

    A conditional type has the shape T extends U ? A : B and is evaluated at the type level: if the source type is assignable to the target, you get A; otherwise B. The most consequent...

  4. 4Mapped Types and Template Literal Types

    A mapped type walks the keys of an existing type and produces a new one, in the form { [K in keyof T]: ... }. The built-ins Partial<T>, Required<T>, and Readonly<T&g...

  5. 5Narrowing, Type Guards, and Branded Types

    TypeScript narrows a value's type within control-flow branches using predicates and operators. A user-defined type guard has the form function isString(x: unknown): x is string { r...

  6. 6Tuple and Variadic Generics

    Tuples let generic types represent fixed-shape arrays precisely, and TypeScript extends this idea with variadic tuple types—tuples containing rest elements like [string, ...number[...

  7. 7Advanced Patterns, Ecosystem, and Tooling

    Several language features elevate generics from "type-safe containers" into full API design tools. The satisfies operator validates that a value conforms to a shape without forcing...

← Back to the Typescript Generics Patterns And Constraints deck