0
0
Typescriptprogramming~20 mins

Type-safe builder pattern in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type-safe Builder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
AError: Missing properties
B{ color: 'red', wheels: 4 }
C{ color: undefined, wheels: 4 }
D{ color: 'red', wheels: undefined }
Attempts:
2 left
💡 Hint
Check if all required properties are set before building.
🧠 Conceptual
intermediate
1:30remaining
Purpose of type-safe builder pattern
What is the main advantage of using a type-safe builder pattern in TypeScript?
AIt allows creating objects without specifying any properties.
BIt automatically generates UI forms for object creation.
CIt makes the code run faster by skipping type checks.
DIt ensures all required properties are set before object creation, preventing runtime errors.
Attempts:
2 left
💡 Hint
Think about how type safety helps prevent mistakes.
🔧 Debug
advanced
2: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);
A{ name: 'Alice', age: undefined }
BTypeError at runtime
C{ name: 'Alice', age: number }
DError: Missing properties
Attempts:
2 left
💡 Hint
Check the condition in the if statement inside build().
📝 Syntax
advanced
2: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?
A
class BookBuilder {
  private title?: string;
  private author?: string;

  setTitle(title: string) {
    this.title = title;
    return this;
  }

  setAuthor(author: string) {
    this.author = author;
    return this;
  }

  build(): { title: string; author: string } {
    if (!this.title || !this.author) {
      throw new Error('Missing properties');
    }
    return { title: this.title, author: this.author };
  }
}
B
class BookBuilder {
  private title: string;
  private author: string;

  setTitle(title: string) {
    this.title = title;
    return this;
  }

  setAuthor(author: string) {
    this.author = author;
    return this;
  }

  build(): { title: string; author: string } {
    return { title: this.title, author: this.author };
  }
}
C
class BookBuilder {
  private title?: string;
  private author?: string;

  setTitle(title: string) {
    this.title = title;
    return this;
  }

  setAuthor(author: string) {
    this.author = author;
    return this;
  }

  build(): { title: string; author: string } {
    return { title: this.title!, author: this.author! };
  }
}
D
class BookBuilder {
  private title?: string;
  private author?: string;

  setTitle(title: string) {
    this.title = title;
    return this;
  }

  setAuthor(author: string) {
    this.author = author;
    return this;
  }

  build(): { title: string; author: string } {
    if (this.title === undefined || this.author === undefined) {
      throw new Error('Missing properties');
    }
    return { title: this.title!, author: this.author! };
  }
}
Attempts:
2 left
💡 Hint
Check the condition that throws an error if any required property is missing.
🚀 Application
expert
3: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();
AExactly 2, one with label and one without.
BOnly 1, because label is optional and x,y must be fixed.
CInfinite, because x and y can be any number and label is optional.
DZero, because the builder does not set any default values.
Attempts:
2 left
💡 Hint
Consider the range of possible values for x and y.