Concept Flow - Generic factory pattern
Call factory with type T
Create instance of T
Return new instance
Use instance in code
The factory receives a type and creates a new instance of that type, then returns it for use.
class Car { drive() { return 'Driving'; } } function factory<T>(c: new () => T): T { return new c(); } const car = factory(Car); console.log(car.drive());
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Call factory with Car | factory<Car>(Car) | Enter factory function |
| 2 | Create new instance | new Car() | Car instance created |
| 3 | Return instance | return new Car() | Car instance returned |
| 4 | Assign to variable | const car = factory(Car) | car holds Car instance |
| 5 | Call method | car.drive() | 'Driving' returned |
| 6 | Output result | console.log(car.drive()) | Prints: Driving |
| Variable | Start | After Step 2 | After Step 4 | Final |
|---|---|---|---|---|
| car | undefined | Car instance created | Car instance assigned | Car instance with drive method |
Generic factory pattern in TypeScript: - Use a generic function with a constructor signature: function factory<T>(c: new () => T): T - Inside, create instance with 'new c()' - Returns a new instance of type T - Allows creating objects without knowing exact class at compile time - Works for classes with no-argument constructors
What is the main purpose of the Generic factory pattern in TypeScript?
Which of the following is the correct syntax to declare a generic factory function in TypeScript?
function create<T>(): T { /* ... */ }T and returns an object of type T.{} as T which is valid to create a generic placeholder object. function create<T>(): T { return new T(); } tries to use new T() which is invalid because T is a type parameter, not a constructor. function create<T>(): T { return T(); } calls T() like a function which is invalid. function create<T>(): T { return new Object(); } returns a generic object but not typed as T.What will be the output of this TypeScript code using a generic factory pattern?
interface Animal { speak(): string; }
class Dog implements Animal { speak() { return "Woof"; } }
function createAnimal<T extends Animal>(ctor: new () => T): T {
return new ctor();
}
const dog = createAnimal(Dog);
console.log(dog.speak());createAnimal takes a constructor ctor and returns a new instance of type T which extends Animal.Dog class to createAnimal, so it returns a new Dog instance. Calling dog.speak() returns "Woof".Identify the error in this generic factory function and select the fix.
function createInstance<T>(): T {
return new T();
}new T() causes errornew T() because T is a type parameter, not a constructor function.ctor: new () => T allows calling new ctor() to create an instance of T.ctor: new () => T and return new ctor(). -> Option AYou want to create a generic factory that can produce different shapes with a draw() method. Which approach correctly uses the generic factory pattern to support adding new shapes easily?
interface Shape { draw(): void; }
class Circle implements Shape { draw() { console.log("Circle"); } }
class Square implements Shape { draw() { console.log("Square"); } }
// What is the best factory implementation?Shape without hardcoding types.T, which is unsafe.Pass the test and this pattern moves unknown