Factory functions and generic construction
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Write a factory that infers its product type.
- Constrain a constructor parameter with new.
- Return a typed object without classes.
Overview
#A simple factory
function makeUser(name: string) {
return { name, createdAt: Date.now() };
}
type User = ReturnType<typeof makeUser>;
The factory result is the single source of truth for the type.
Generic factory
function pair<A, B>(a: A, b: B): [A, B] {
return [a, b];
}
Constructor constraint
type Ctor<T> = new (...args: any[]) => T;
function create<T>(Ctor: Ctor<T>): T {
return new Ctor();
}
new (...args) => T describes anything constructable, which lets a factory accept a class and return its instance type.
Factory with defaults
function withDefaults<T extends object>(defaults: T, overrides: Partial<T>): T {
return { ...defaults, ...overrides };
}
Why factories
Factories hide construction details, return plain data, and let the return type drive the API. They compose better than classes when there is no behavior to encapsulate.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Implement `create<T>(Ctor: new () => T): T` that returns a new instance.
starter.typescriptexport function create<T>(Ctor: new () => T): T { // your code }Reveal the reference answer
reference.typescriptexport function create<T>(Ctor: new () => T): T { return new Ctor(); }Authored expected output: Compiles with no errors. Runtime: create(class {}) returns an object instance.
The construct-signature constraint lets the body call new and returns the instance type.
Exercise 2Predict
What is `User`? ```ts function makeUser(name: string) { return { name, active: true }; } type User = ReturnType<typeof makeUser>; ```
Exercise 3Debug
This fails because the constraint accepts any function, not just constructors. Fix it. ```ts type Ctor<T> = (...args: any[]) => T; function create<T>(Ctor: Ctor<T>): T { return new Ctor(); } ```
starter.typescripttype Ctor<T> = (...args: any[]) => T; function create<T>(Ctor: Ctor<T>): T { return new Ctor(); }Reveal the reference answer
reference.typescripttype Ctor<T> = new (...args: any[]) => T; function create<T>(Ctor: Ctor<T>): T { return new Ctor(); }Authored expected output: Compile error resolved: new cannot be used on a non-constructor. The new (...args: any[]) => T constraint fixes it.
Only a construct signature permits the new operator.
Common misconceptions
#- A factory must return a class instance.
- ReturnType works on a value without typeof.
- The new constraint accepts any function.
Follow-up practice
#- typescript-builder-pattern
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.