UserProfile with name (string), age (number), and optional email (string).UserProfileBuilder that allows setting name, age, and email.name and age must be set before build() is callable.UserProfile with name (string), age (number), and optional email (string).UserProfileBuilder that allows setting name, age, and email.name and age must be set before build() is callable.UserProfile with these exact properties: name of type string, age of type number, and optional email of type string.Use the interface keyword and mark email as optional with ?.
UserProfileBuilder with two generic boolean parameters NameSet and AgeSet both defaulting to false. Add private properties _name (string or undefined), _age (number or undefined), and _email (string or undefined). Add methods setName(name: string) returning UserProfileBuilder<true, AgeSet> and setAge(age: number) returning UserProfileBuilder<NameSet, true>. Also add setEmail(email: string) returning this. Do not implement build() yet.Use generics with default false and return this casted to the new generic type for setName and setAge.
build() that returns UserProfile. Use a conditional type to allow build() only if both NameSet and AgeSet are true. Otherwise, it should cause a TypeScript error. Inside build(), return an object with name, age, and email from the private properties.Use build(this: UserProfileBuilder<true, true>) to restrict calling build() only when both flags are true.
profile by calling new UserProfileBuilder(), then chaining setName("Alice"), setAge(30), and setEmail("alice@example.com"), and finally calling build(). Then print profile using console.log(profile).Chain the methods exactly as new UserProfileBuilder().setName("Alice").setAge(30).setEmail("alice@example.com").build() and print the result.
What is the main benefit of using a type-safe builder pattern in TypeScript?
Which of the following is the correct way to declare a generic builder class in TypeScript?
class Builder<T> {
private data: T;
constructor() {
this.data = {} as T;
}
build(): T {
return this.data;
}
}<T> and initializes data as {} as T.build() method returns data typed as T, matching the generic type.<T> and proper initialization [OK]Consider this TypeScript code using a type-safe builder pattern:
interface User {
name: string;
age: number;
}
class UserBuilder {
private user = {} as Partial<User>;
setName(name: string) { this.user.name = name; return this; }
setAge(age: number) { this.user.age = age; return this; }
build(): User {
if (!this.user.name || !this.user.age) throw new Error('Missing fields');
return this.user as User;
}
}
const user = new UserBuilder().setName('Alice').setAge(30).build();
console.log(user);What will be the output?
name to 'Alice' and age to 30, so both required fields are set.User.Identify the error in this type-safe builder pattern code and select the fix:
interface Car {
make: string;
year: number;
}
class CarBuilder {
private car = {} as Partial<Car>;
setMake(make: string) { this.car.make = make; return this; }
setYear(year: number) { this.car.year = year; return this; }
build(): Car {
if (!this.car.make || !this.car.year) throw new Error('Missing fields');
return this.car;
}
}this.car which is typed as Partial<Car>, but the return type is Car.this.car as Car after checking required fields.You want to create a type-safe builder for a Product with required id and name, and optional description. Which approach best enforces setting required fields before build?
interface Product {
id: number;
name: string;
description?: string;
}
// Which builder pattern enforces required fields at compile time?Pass the test and this pattern moves unknown