0Pricing
TypeScript Academy · Lesson

Immutable Builders

Return new builder instances for safer composition.

Mutation vs Immutability

The builders so far mutate this and return it. An immutable builder instead returns a new builder instance on every step, never changing the original. This makes composition safer.

The Problem with Shared Mutation

If two parts of your code hold the same mutable builder, one setter call affects both. Immutable builders avoid this aliasing surprise entirely.

const base = makeMutableBuilder().url("/x");
// If base is shared, later .method() calls mutate everyone's copy.

All lessons in this course

  1. The Builder Pattern Basics
  2. Fluent Interfaces with Types
  3. Enforcing Required Steps
  4. Immutable Builders
← Back to TypeScript Academy