← All Exercises

Type-Safe Builder Pattern

intermediatetypescript~20min

Background

The builder pattern with type safety prevents runtime errors by enforcing method call order at compile time.

Problem

Create a builder class that uses TypeScript types to enforce required methods before build can be called.

Input

Builder method calls

Output

Constructed object

Sample tests

Sample 1

Input: new Builder().setName("x").build()
Expected: object with name

Your solution

Learning objective

Learn fluent interfaces and type-safe chaining