TypeScript Cookbook — Recipes

Over 100 recipes from The TypeScript Cookbook, each with a working TypeScript example you can open and explore in the TypeScript Playground.

Chapter 1: Project Setup

  1. Type-Checking JavaScript
  2. Installing TypeScript
  3. Keeping Types on the Side
  4. Migrating a Project to TypeScript
  5. Loading Types from Definitely Typed
  6. Setting Up a Full-Stack Project
  7. Setting Up Tests
  8. Typing ECMAScript Modules from URLs
  9. Loading Different Module Types in Node
  10. Working with Deno and Dependencies

Chapter 2: Basic Types

  1. Annotating Effectively
  2. Working with any and unknown
  3. Working with Tuple Types
  4. Understanding Interfaces Versus Type Aliases
  5. Defining Function Overloads
  6. Defining this Parameter Types
  7. Working with Symbols
  8. Understanding Value and Type Namespaces
  9. Choosing the Right Object Type

Chapter 3: The Type System

  1. Modeling Data with Union and Intersection Types
  2. Explicitly Defining Models with Discriminated Union Types
  3. Exhaustiveness Checking with the Assert never Technique
  4. Pinning Types with Const Context
  5. Narrowing Types with Type Predicates
  6. Understanding void
  7. Dealing with Error Types in catch Clauses
  8. Creating Exclusive Or Models with Optional never
  9. Using Index Signatures
  10. Effectively Using Type Assertions
  11. Working with Enums
  12. Defining Nominal Types in a Structural Type System
  13. Enabling Loose Autocomplete for String Subsets
  14. Distinguishing Missing Properties and Undefined Values

Chapter 4: Generics

  1. Generalizing Function Signatures
  2. Creating Related Function Arguments
  3. Getting Rid of any and unknown
  4. Using ThisType to Define this in Objects
  5. Generating New Object Types
  6. Understanding Generic Instantiation
  7. Modifying Objects with Assertion Signatures
  8. Mapping Types with Type Maps
  9. Adding Const Context to Generic Type Parameters

Chapter 5: Conditional Types

  1. Managing Complex Function Signatures
  2. Filtering with never
  3. Grouping Elements by Kind
  4. Removing Specific Object Properties
  5. Inferring Types in Conditionals

Chapter 6: String Template Literal Types

  1. Defining a Custom Event System
  2. Creating Event Callbacks with String Manipulation Types and Key Remapping
  3. Writing a Formatter Function
  4. Extracting Format Parameter Types
  5. Dealing with Recursion Limits
  6. Using Template Literals as Discriminants

Chapter 7: Variadic Tuple Types

  1. Typing a concat Function
  2. Typing a promisify Function
  3. Typing a curry Function
  4. Typing a Flexible curry Function
  5. Typing the Simplest curry function
  6. Creating an Enum from a Tuple
  7. Splitting All Elements of a Function Signature

Chapter 8: Helper Types

  1. Setting Specific Properties Optional
  2. Modifying Nested Objects
  3. Remapping Types
  4. Getting All Required Keys
  5. Allowing at Least One Property
  6. Converting Union to Intersection Types
  7. Using type-fest
  8. Allowing Exactly One and All or None

Chapter 9: The Standard Library and External Type Definitions

  1. Iterating over Objects with Object.keys
  2. Explicitly Highlighting Unsafe Operations with Type Assertions and unknown
  3. Working with defineProperty
  4. Expanding Types for Array.prototype.includes
  5. Extending Modules
  6. Augmenting Globals
  7. Adding Non-JS Modules to the Module Graph
  8. Filtering Nullish Values

Chapter 10: TypeScript and React

  1. Writing Proxy Components
  2. Typing Custom Hooks
  3. Typing Generic forwardRef Components
  4. Providing Types for the Context API
  5. Typing Higher-Order Components
  6. Typing Callbacks in React's Synthetic Event System
  7. Typing Polymorphic Components
  8. Writing Controlled Components

Chapter 11: Classes

  1. Choosing the Right Visibility Modifier
  2. Explicitly Defining Method Overrides
  3. Describing Constructors and Prototypes
  4. Using Generics in Classes
  5. Writing Static Classes
  6. Deciding When to Use Classes or Namespaces
  7. Working with Strict Property Initialization
  8. Working with this Types in Classes
  9. Writing Decorators

Chapter 12: Type Development Strategies

  1. Writing Low Maintenance Types
  2. Refining Types Step by Step
  3. Checking Contracts with satisfies
  4. Testing Complex Types
  5. Validating Data Types at Runtime with Zod
  6. Working Around Index Access Restrictions
  7. Deciding Whether to Use Function Overloads or Conditional Types
  8. Naming Generics
  9. Prototyping on the TypeScript Playground
  10. Providing Multiple Library Versions