0Pricing
TypeScript Academy · Lesson

Enforcing Required Steps

Prevent build() until all required fields are set.

Enforcing Required Steps is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 };

Conditional build Signature

We make build available only when the accumulated state S extends the required keys. A conditional type returns the real method or an unusable one.

class Builder<S> {
  build(this: Builder<Required>): Config {
    return this.d as Config;
  }
  private d: Record<string, unknown> = {};
}

Using the this Parameter

The this: Builder<Required> parameter constrains which builder instances may call build. If S does not satisfy Required, the call is a type error.

// Only a Builder whose S includes url and method
// can legally invoke build().

Wiring Setters to State

Each setter widens S as before. After both required setters run, S finally extends Required and build becomes callable.

class Builder<S> {
  private d: Record<string, unknown> = {};
  url(u: string): Builder<S & { url: string }> { this.d.url = u; return this as any; }
  method(m: string): Builder<S & { method: string }> { this.d.method = m; return this as any; }
  build(this: Builder<{ url: string; method: string }>): Config { return this.d as Config; }
}

The Happy Path Compiles

When both required steps are present the chain type-checks and runs. The compiler is satisfied that S includes every required key.

const cfg = new Builder<{}>().url("/x").method("GET").build();
console.log(cfg.url, cfg.method);

The Unhappy Path Fails

Omit a required step and build() no longer type-checks, because the builder's S does not extend Required. The mistake is caught at compile time.

// const bad = new Builder<{}>().url("/x").build();
// Error: 'build' can only be called when method is set too.

Why this Beats Runtime Guards

A runtime if (!this.url) throw only fails when the code runs. The this-parameter approach fails during type checking, so incomplete builds never ship.

Optional Fields Stay Optional

Optional steps like timeout are never part of Required, so they do not gate build(). You can include or skip them freely.

const ok = new Builder<{}>().method("PUT").url("/y").build();
console.log("order of required steps does not matter");

Scaling to Many Requirements

Add more mandatory keys to the Required shape and the this parameter, and the compiler enforces them all. The pattern scales to any number of required steps.

Limitations

The guarantee depends on threading S honestly through every setter and using this-parameter typing. Casts must be confined to the setters so callers stay fully type-safe.

Quick Check

Quick check on this lesson.

Recap

To enforce required steps, type build with a this: Builder<Required> parameter. The method only type-checks once the accumulated state S includes every required key, turning incomplete construction into a compile error.

Frequently asked questions

Is the “Enforcing Required Steps” lesson free?

Yes — the full text of “Enforcing Required Steps” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Enforcing Required Steps”?

Prevent build() until all required fields are set. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Enforcing Required Steps” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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