Concept Flow - Builder pattern with generics
Start Builder
Set Property 1
Set Property 2
Set Property N
Call build()
Return Built Object
The builder starts empty, sets properties one by one, then builds the final object.
class Builder<T> { private obj: Partial<T> = {}; set<K extends keyof T>(key: K, value: T[K]): this { this.obj[key] = value; return this; } build(): T { return this.obj as T; } }
| Step | Action | Key | Value | Object State | Return |
|---|---|---|---|---|---|
| 1 | Create Builder | - | - | {} | Builder instance |
| 2 | Set property | name | "Alice" | {"name": "Alice"} | Builder instance |
| 3 | Set property | age | 30 | {"name": "Alice", "age": 30} | Builder instance |
| 4 | Build object | - | - | {"name": "Alice", "age": 30} | {"name": "Alice", "age": 30} |
| Variable | Start | After 1 | After 2 | Final |
|---|---|---|---|---|
| obj | {} | {"name": "Alice"} | {"name": "Alice", "age": 30} | {"name": "Alice", "age": 30} |
Builder pattern with generics: - Use a generic class Builder<T> with Partial<T> to hold properties. - set<K extends keyof T>(key, value) sets properties and returns 'this' for chaining. - build() returns the final object as type T. - Allows step-by-step object creation with type safety.
What is the main advantage of using the builder pattern with generics in TypeScript?
Which of the following is the correct syntax to declare a generic builder class in TypeScript?
class Builder<T> {
private obj: T;
constructor() {
this.obj = {} as T;
}
set(key: keyof T, value: T[keyof T]): this {
this.obj[key] = value;
return this;
}
build(): T {
return this.obj;
}
}<T> and initializes obj as {} as T.new T() which is invalid in TypeScript. class Builder { private obj: T; constructor() { this.obj = {} as T; } } misses generic <T>. class Builder<T> { private obj: any; constructor() { this.obj = {}; } } uses any losing type safety.{} as T init = class Builder<T> { private obj: T; constructor() { this.obj = {} as T; } } [OK]{} as T to initialize generic object safely [OK]What will be the output of this code?
class Builder<T> {
private obj: T;
constructor() { this.obj = {} as T; }
set(key: keyof T, value: T[keyof T]): this {
this.obj[key] = value;
return this;
}
build(): T { return this.obj; }
}
interface Person { name: string; age: number; }
const person = new Builder<Person>()
.set('name', 'Alice')
.set('age', 30)
.build();
console.log(person);build() method returns the object with both properties set, so output is { name: 'Alice', age: 30 }.Identify the error in this builder class and select the fix:
class Builder<T> {
private obj: T;
constructor() { this.obj = {}; }
set(key: keyof T, value: T[keyof T]): this {
this.obj[key] = value;
return this;
}
build(): T { return this.obj; }
}{} directly to obj: T causes a type error because {} is not guaranteed to be T.{} as T tells TypeScript to treat empty object as type T safely during construction.{} as T in constructor. -> Option D{} as T to fix generic object init [OK]{} as T to initialize generic objects [OK]You want to build a generic builder that only allows setting properties defined in the object type and prevents setting undefined keys. Which TypeScript feature helps enforce this at compile time?
keyof T for key typekeyof T represents all valid keys of type T, so using it restricts keys to only those defined in T.keyof T as the key type in the set method. -> Option Bkeyof T restricts keys to valid properties [OK]keyof T to limit keys to object properties [OK]Pass the test and this pattern moves unknown