Challenge - 5 Problems
Type-safe Builder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple type-safe builder
What is the output of this TypeScript code using a type-safe builder pattern?
Typescript
class CarBuilder { private color?: string; private wheels?: number; setColor(color: string) { this.color = color; return this; } setWheels(wheels: number) { this.wheels = wheels; return this; } build(): { color: string; wheels: number } { if (!this.color || !this.wheels) { throw new Error('Missing properties'); } return { color: this.color, wheels: this.wheels }; } } const car = new CarBuilder().setColor('red').setWheels(4).build(); console.log(car);
Attempts:
2 left
💡 Hint
Check if all required properties are set before building.
✗ Incorrect
The builder sets both color and wheels before calling build(), so the returned object has both properties defined.
🧠 Conceptual
intermediate1:30remaining
Purpose of type-safe builder pattern
What is the main advantage of using a type-safe builder pattern in TypeScript?
Attempts:
2 left
💡 Hint
Think about how type safety helps prevent mistakes.
✗ Incorrect
Type-safe builders enforce setting required properties at compile time, reducing runtime errors.
🔧 Debug
advanced2:30remaining
Identify the error in this builder pattern
What error will this TypeScript code produce when calling build()?
Typescript
class UserBuilder { private name?: string; private age?: number; setName(name: string) { this.name = name; return this; } setAge(age: number) { this.age = age; return this; } build(): { name: string; age: number } { if (!this.name && !this.age) { throw new Error('Missing properties'); } return { name: this.name!, age: this.age! }; } } const user = new UserBuilder().setName('Alice').build(); console.log(user);
Attempts:
2 left
💡 Hint
Check the condition in the if statement inside build().
✗ Incorrect
The condition only throws if both name and age are missing, but age is missing here, so build returns an object with age undefined.
📝 Syntax
advanced2:30remaining
Which option correctly enforces required properties in builder?
Which TypeScript code snippet correctly enforces that 'title' and 'author' must be set before calling build() in a type-safe builder?
Attempts:
2 left
💡 Hint
Check the condition that throws an error if any required property is missing.
✗ Incorrect
Option A throws an error if either title or author is missing, correctly enforcing required properties before build.
🚀 Application
expert3:00remaining
How many valid objects can this type-safe builder create?
Given this TypeScript type-safe builder that requires setting 'x' and 'y' as numbers and optionally 'label' as string, how many distinct valid objects can be created?
Typescript
class PointBuilder { private x?: number; private y?: number; private label?: string; setX(x: number) { this.x = x; return this; } setY(y: number) { this.y = y; return this; } setLabel(label: string) { this.label = label; return this; } build(): { x: number; y: number; label?: string } { if (this.x === undefined || this.y === undefined) { throw new Error('Missing required properties'); } return this.label === undefined ? { x: this.x, y: this.y } : { x: this.x, y: this.y, label: this.label }; } } const builder = new PointBuilder();
Attempts:
2 left
💡 Hint
Consider the range of possible values for x and y.
✗ Incorrect
Since x and y are numbers and can be any value, and label is optional, the number of valid objects is infinite.