0
0
Typescriptprogramming~20 mins

Builder pattern with generics in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Builder Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a generic builder pattern example
What is the output of this TypeScript code using a generic builder pattern?
Typescript
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;
  }
}

interface Person {
  name: string;
  age: number;
}

const person = new Builder<Person>()
  .set('name', 'Alice')
  .set('age', 30)
  .build();

console.log(person);
ASyntaxError
B{"name":"Alice"}
C{"name":"Alice","age":30}
DTypeError at runtime
Attempts:
2 left
💡 Hint
Look at how the generic Builder class stores and returns the object.
Predict Output
intermediate
2:00remaining
What error occurs with missing required property?
Given this generic builder pattern code, what error occurs when a required property is missing?
Typescript
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;
  }
}

interface Car {
  make: string;
  year: number;
}

const car = new Builder<Car>()
  .set('make', 'Toyota')
  .build();

console.log(car.year.toString());
ACompile-time error in TypeScript
BSyntaxError: Missing property 'year'
CNo error, outputs 'undefined'
DTypeError: Cannot read property 'toString' of undefined
Attempts:
2 left
💡 Hint
Check what happens when you access a property that was never set.
🔧 Debug
advanced
2:00remaining
Identify the bug in this generic builder pattern
What is the bug in this TypeScript generic builder pattern code?
Typescript
class Builder<T> {
  private obj: T = {} as T;

  set<K extends keyof T>(key: K, value: T[K]): this {
    this.obj[key] = value;
    return this;
  }

  build(): T {
    return this.obj;
  }
}

interface Book {
  title: string;
  pages: number;
}

const book = new Builder<Book>()
  .set('title', 'My Book')
  .set('pages', 100)
  .build();

console.log(book);
ANo error, outputs { title: 'My Book', pages: 100 }
BTypeError: Cannot assign to 'key' because 'obj' is not initialized properly
CSyntaxError: Missing colon in class property
DRuntime error: obj is undefined
Attempts:
2 left
💡 Hint
Check how obj is initialized and if {} is assignable to T.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a generic builder method?
Which of these method signatures correctly defines a generic set method in a builder pattern class in TypeScript?
Aset<K extends keyof T>(key: K, value: T[K]): this
Bset(key: string, value: any): this
Cset<K>(key: K, value: T[K]): this
Dset(key: keyof T, value: any): T
Attempts:
2 left
💡 Hint
Look for correct generic constraints and return type.
🚀 Application
expert
2:00remaining
How many properties does the built object have?
Consider this generic builder pattern code. How many properties does the final object have?
Typescript
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;
  }
}

interface Config {
  host: string;
  port: number;
  secure: boolean;
}

const config = new Builder<Config>()
  .set('host', 'localhost')
  .set('port', 8080)
  .build();

console.log(Object.keys(config).length);
A2
B3
C0
DRuntime error
Attempts:
2 left
💡 Hint
Only properties set by set() exist in the object.