0
0
Typescriptprogramming~20 mins

Generic factory pattern in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Factory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this generic factory function?

Consider the following TypeScript code using a generic factory pattern. What will be printed to the console?

Typescript
function createInstance<T>(ctor: new () => T): T {
  return new ctor();
}

class Dog {
  speak() { return "Woof!"; }
}

const dog = createInstance(Dog);
console.log(dog.speak());
A"Woof!"
Bundefined
CTypeError at runtime
DCompilation error
Attempts:
2 left
💡 Hint

The factory function creates an instance using the constructor passed in. What does dog.speak() return?

🧠 Conceptual
intermediate
1:30remaining
Which statement about generic factory pattern is true?

Choose the correct statement about the generic factory pattern in TypeScript.

AIt allows creating instances of any class type passed as a constructor parameter.
BIt requires the class to implement a specific interface with a factory method.
CIt can only create instances of classes without constructor parameters.
DIt cannot be used with classes that have private constructors.
Attempts:
2 left
💡 Hint

Think about how generics and constructors work in TypeScript.

🔧 Debug
advanced
2:00remaining
Why does this generic factory code cause a compilation error?

Examine the following TypeScript code. Why does it cause a compilation error?

Typescript
function createInstance<T>(ctor: new (name: string) => T, name: string): T {
  return new ctor();
}

class Cat {
  constructor(public name: string) {}
}

const cat = createInstance(Cat, "Whiskers");
AThe class Cat does not have a default constructor.
BThe generic type T is not specified explicitly.
CThe constructor is called without the required 'name' argument.
DThe function createInstance cannot accept parameters.
Attempts:
2 left
💡 Hint

Look at how the constructor is called inside the function.

📝 Syntax
advanced
2:00remaining
Which option correctly types a generic factory function for classes with parameters?

Choose the correct TypeScript signature for a generic factory function that can create instances of classes with any constructor parameters.

Afunction createInstance<T>(ctor: new (...args: T[]) => T): T
Bfunction createInstance<T>(ctor: new () => T, args: any[]): T
Cfunction createInstance<T>(ctor: T, ...args: any[]): T
Dfunction createInstance<T>(ctor: new (...args: any[]) => T, ...args: any[]): T
Attempts:
2 left
💡 Hint

Consider how to type a constructor with any number of parameters.

🚀 Application
expert
2:30remaining
How many items are in the resulting map after using this generic factory?

Given the following TypeScript code using a generic factory to create multiple instances, how many entries does the instances map contain?

Typescript
class User {
  constructor(public id: number) {}
}

class Product {
  constructor(public id: number) {}
}

function createInstance<T>(ctor: new (id: number) => T, id: number): T {
  return new ctor(id);
}

const instances = new Map<string, unknown>();
instances.set("user1", createInstance(User, 1));
instances.set("user2", createInstance(User, 2));
instances.set("product1", createInstance(Product, 1));
instances.set("user1", createInstance(User, 3));
A1
B3
C2
D4
Attempts:
2 left
💡 Hint

Remember how Map keys work when adding entries with the same key.