0Pricing
TypeScript Academy · Lesson

Enforcing Required Steps

Prevent build() until all required fields are set.

Stopping Premature build()

Now we use the accumulated state to forbid calling build() until required fields are set. The type system, not a runtime check, blocks an incomplete build.

Define the Final Shape

Start by declaring the complete object and which keys are mandatory. The builder will only allow build() when the state contains all of them.

interface Config {
  url: string;   // required
  method: string; // required
  timeout?: number; // optional
}
type Required = { url: string; method: string };

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