How about something like:

export interface Base {}

export interface IA extends Base {
    value: string;
}

export type Builders = {
    [name: string]: { new <T extends Base>(): T };
}

export function factory(foo: string): Builders {
    class A implements IA {
        value:string = foo + '-A';
    }

    return { A };
};

And:

import { factory, IA, Builders } from './Factory';

export default class Main {
    private inner: Builders;

    constructor(foo:string) {
        this.inner = factory(foo);
    }

    get a():IA {
        return new this.inner.A() as IA;
    }
}

Edit

What's wrong with this as factory.ts:

export class Base {}

export type Builders = {
    [name: string]: { new <T extends Base>(): T };
}

class A extends Base {
    value: string;

    constructor();
    constructor(foo: string);
    constructor(foo?: string) {
        super();
        this.value = foo + "-A";
    }
}

// more classes...

export function factory(foo: string): Builders {
    return { A: A.bind(A, foo) };
};

It's basically the same as what you did, just that the classes are not defined inside the class, and are exported so no need for the interfaces I suggested.
Also, this way all the classes will only be evaluated once and not every time the factory function is invoked.

Answer from Nitzan Tomer on Stack Overflow
🌐
Refactoring.Guru
refactoring.guru › home › design patterns › factory method › typescript
Factory Method in TypeScript / Design Patterns
January 1, 2026 - Full code example in TypeScript with detailed comments and explanation. Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes.
🌐
Zabbix
sbcode.net › typescript › factory
Factory - Design Patterns in TypeScript
The Factory Pattern defers the creation of the final object to a subclass.
🌐
Bitstack
blog.bitsrc.io › factory-design-pattern-in-typescript-55a91d74f3a4
Factory Design Pattern in TypeScript | Bits and Pieces
March 4, 2025 - The Factory Design Pattern offers a reliable way to encapsulate object creation, thereby enabling system decoupling, enhancing reusability, and providing scalability. When designing systems in TypeScript or any object-oriented language, the ...
🌐
Refactoring.Guru
refactoring.guru › home › design patterns › abstract factory › typescript
Abstract Factory in TypeScript / Design Patterns
January 1, 2026 - Full code example in TypeScript with detailed comments and explanation. Abstract Factory is a creational design pattern, which solves the problem of creating entire product families without specifying their concrete classes.
🌐
Medium
medium.com › @robinviktorsson › a-guide-to-the-factory-design-pattern-in-typescript-and-node-js-with-practical-examples-7390f20f25e7
A Guide to the Factory Design Pattern in TypeScript and Node.js with Practical Examples 💻 | by Robin Viktorsson | Medium
March 10, 2025 - A Guide to the Factory Design Pattern in TypeScript and Node.js with Practical Examples 💻 The Factory design pattern is a creational pattern that provides an interface for creating objects in a …
🌐
Medium
medium.com › @diegomottadev › factory-method-pattern-implementation-using-typescript-and-node-js-6ac075967f22
Factory Method Pattern Implementation Using TypeScript and Node.js | by Diego Motta | Fullstack Developer | Medium
August 25, 2023 - Factory Method Pattern Implementation Using TypeScript and Node.js The Factory Method Pattern offers an elegant solution to a common dilemma: how to create objects when the specific class to …
🌐
DEV Community
dev.to › luizcalaca › typescript-factory-design-pattern-in-practice-uml-6g9
TypeScript Factory Design Pattern in practice (UML) - DEV Community
July 5, 2022 - That's all! We implement a Factory Design Pattern in Typescript and see about the UML representation.
🌐
Medium
medium.com › codex › factory-pattern-type-script-implementation-with-type-map-ea422f38862
Factory Pattern In TypeScript Using Type Map | by Sunny Sun | CodeX | Medium
May 12, 2024 - Factory pattern is one of the most common creation patterns. It loosely connects the application by hiding the implementation details from the client code using the interface. It leaves the creation of object instances to the factory implementation.
🌐
YouTube
youtube.com › watch
TypeScript Factory Patterns (No BS TS Series 2 Episode 1) - YouTube
No BS TS is back! With the first in an all new series where we cover all the patterns in the original Design Patterns book (and more). This time we are looki...
Published   September 20, 2021
Find elsewhere
Top answer
1 of 2
8

How about something like:

export interface Base {}

export interface IA extends Base {
    value: string;
}

export type Builders = {
    [name: string]: { new <T extends Base>(): T };
}

export function factory(foo: string): Builders {
    class A implements IA {
        value:string = foo + '-A';
    }

    return { A };
};

And:

import { factory, IA, Builders } from './Factory';

export default class Main {
    private inner: Builders;

    constructor(foo:string) {
        this.inner = factory(foo);
    }

    get a():IA {
        return new this.inner.A() as IA;
    }
}

Edit

What's wrong with this as factory.ts:

export class Base {}

export type Builders = {
    [name: string]: { new <T extends Base>(): T };
}

class A extends Base {
    value: string;

    constructor();
    constructor(foo: string);
    constructor(foo?: string) {
        super();
        this.value = foo + "-A";
    }
}

// more classes...

export function factory(foo: string): Builders {
    return { A: A.bind(A, foo) };
};

It's basically the same as what you did, just that the classes are not defined inside the class, and are exported so no need for the interfaces I suggested.
Also, this way all the classes will only be evaluated once and not every time the factory function is invoked.

2 of 2
7

Your code (the OP's) is working just fine in July 2019 with Typescript 3.5.2.

PS. I got here when I was trying to create a Class Factory in Typescript.

Things to keep in mind:

  1. The concept of Class definition in Typescript has two folds. One is the instance type, and the other one is the constructor function. (source in TS Docs)
  2. Since Typescript 2.8 released in March 2018, you can explicitly create the instance type from the constructor function using InstanceType. e.g. type classType = InstanceType<typeof C>. This should make the explicit type definition in the selected answer simpler.
  3. Finally, I got my code working using a simple trick as following:
export default class MyNewClass
   extends ClassFactory("custom param1") {}

This way I am actually defining the generated class as a new class by itself, so both the constructor function and the instance-type type are going to be implicitly created and will be valid. And I don't need to explicitly define the two separately.

🌐
Wanago
wanago.io › home › javascript design patterns #2. factories and their implementation in typescript
JavaScript design patterns #2. Factories and their implementation in TypeScript
December 2, 2019 - Thanks to proper typing, TypeScript now knows that the codingTeacher, for example, is an instance of the CodingTeacher class. error TS2339: Property ‘instrument’ does not exist on type ‘CodingTeacher’. In this article, we’ve managed to shed some light on what a factory method pattern is and how it shouldn’t be confused with factory functions.
🌐
HackerNoon
hackernoon.com › the-factory-method-pattern-in-typescript
The Factory Method pattern in TypeScript | HackerNoon
December 13, 2022 - The Factory Method pattern is a design pattern that is used to create objects without specifying the exact class of object that will be created.
🌐
DEV Community
dev.to › jmalvarez › factory-method-pattern-in-typescript-2a1n
Factory Method pattern in TypeScript - DEV Community
December 21, 2022 - The factory method pattern is a creational pattern which provides an interface to create objects in a parent class.
🌐
GitHub
github.com › AliN11 › typescript-design-patterns › blob › main › patterns › factory-method.ts
typescript-design-patterns/patterns/factory-method.ts at main · AliN11/typescript-design-patterns
Real world examples of design patterns written in TypeScript. Read a deep, detailed explanation of these patterns here: https://ditty.ir/series/design-patterns (In Persian) - AliN11/typescript-desi...
Author   AliN11
🌐
Just Code
justcode.me › home › factory pattern in typescript: simplify coding secrets
Factory Pattern in TypeScript: Simplify Coding Secrets
March 12, 2024 - Learn the Factory Pattern with TypeScript: a guide to creating scalable and maintainable code through a practical notification system example
🌐
Bitstack
blog.bitsrc.io › abstract-factory-pattern-in-typescript-8c3c5b71206d
Abstract Factory Design Pattern in TypeScript | by Mehmet Yildiz ...
June 19, 2024 - It is a creational pattern that provides an interface for creating related or dependent objects without specifying their concrete classes. ... The Abstract Factory Pattern allows a system to be independent of how its objects are created, composed, ...
🌐
Cloudaffle
cloudaffle.com › factory pattern
The Factory Pattern In TypeScript | Cloudaffle | Everything About Web Development, JavaScript Tutorials, Tips and Tricks
As you can see, the Factory pattern allows us to create different types of Car objects without directly instantiating classes. Instead, we use the CarFactory to create objects, making it easier to add new Car types without modifying the client code. Level Up Your TypeScript And Object Oriented Programming Skills.
🌐
Stack Overflow
stackoverflow.com › questions › 72070693 › factory-method-design-pattern-typescript
Factory method design pattern Typescript - Stack Overflow
The original code example uses the abstract class Creator to establish the contract which all concrete creators must fulfill. It says: "in order to be a Creator, you must have a factoryMethod that returns a Product".
🌐
JavaScript in Plain English
javascript.plainenglish.io › design-patterns-with-typescript-factory-method-cb190d7ce275
Design Patterns with TypeScript: Factory Method | by Felipe Furlanetti | JavaScript in Plain English
January 31, 2022 - Design Patterns with TypeScript: Factory Method The factory method is a creational design pattern whose goal is to allow us to delegate the creation of a class to a method. Doing this allows us to …
🌐
Medium
medium.com › @ibrahimsengun › typescript-factory-design-pattern-ce7b72c2ffd7
TypeScript Factory Design Pattern | by Ibrahim sengun | Medium
December 29, 2023 - TypeScript Factory Design Pattern What is factory design pattern? The Factory Design Pattern is a creational design pattern that manipulates the creation of objects from superclasses. It places an …