Factory
A factory is a function (or static method) that returns a new object without the caller using new on a concrete class. In TypeScript you type the input as a discriminant or a map of constructors, and the return type follows the input.
Detailed Explanation
The point of a factory is to hide which concrete type is created. The TypeScript point is to not hide it from the type system. If the caller passed "admin", they should get Admin, not User.
Three typings show up.
A discriminated input: function create(kind: "user"): User; function create(kind: "admin"): Admin — overloads — or a generic function create<K extends Kind>(kind: K): KindMap[K]. The map form is easier to keep exhaustive.
A constructor map: const registry: { [K in Tag]: new () => Extract<Shape, { tag: K }> }. The factory looks up registry[tag] and returns new Ctor(). The return type is the instance type of that constructor.
A single function that returns a union: create(kind: Kind): User | Admin. This compiles and throws away the useful correlation. Use it only when the caller must handle every kind anyway.
Factories pair well with discriminated unions: the factory is the only place that constructs members, so illegal states stay unrepresentable. They pair well with implements: each product implements an interface, and the factory's return is the interface or the specific class, depending on whether the caller needs specifics.
static create on a class is a factory that can use this as the constructor type so subclasses get the subclass instance. See static members.
Do not type a factory as (): any or (): object. Do not return a class instance and annotate it as an interface if the caller needs extra methods — they will be gone from autocomplete, not from the value.
The JavaScript factory page is about the object-creation pattern. Here the work is overloads vs a Record of constructors vs a correlated generic.
Code Examples
1. Correlated return via a map type.
// 1. Correlated return via a map type.
type User = { role: "user"; email: string };
type Admin = { role: "admin"; email: string; scope: string };
type Kind = "user" | "admin";
type Product = { user: User; admin: Admin };
function create<K extends Kind>(kind: K, email: string): Product[K] {
if (kind === "user") return { role: "user", email } as Product[K];
return { role: "admin", email, scope: "all" } as Product[K];
}
const admin = create("admin", "ada@example.com");
console.log(admin.scope);2. A constructor registry.
// 2. A constructor registry.
interface Animal { speak(): string }
class Dog implements Animal { speak() { return "woof"; } }
class Cat implements Animal { speak() { return "meow"; } }
const ctors = { dog: Dog, cat: Cat };
type Tag = keyof typeof ctors;
function make(tag: Tag): Animal {
return new ctors[tag]();
}3. The weak typing to avoid.
// 3. The weak typing to avoid.
function loose(kind: Kind): User | Admin {
return create(kind, "x@y.z");
}
const maybe = loose("admin");
// maybe.scope — Error, union.Common Gotchas
A union return drops correlation
if the input is a literal, use a generic or overloads so the output is specific.
Implementing the map body often needs `as Product[K]`
TypeScript cannot always prove the `if` narrows the return. Keep the `as` next to a checked discriminant.
`new ctors[tag]()` returns the interface if you annotated the map that way
type the map with `new () => Concrete` if callers need concrete members.
A factory that takes `string` instead of a key union is an `any`-shaped hole
constrain with `keyof`.