How can I return a class from a TypeScript function?

I’m using TypeScript with a dependency injection library which works very similar to Angular 1 – basically: register a factory with your dependencies as arguments.

This is how I would register a class in ES6

export let factory = () => {
    return class Foo {}
};

If I write the same in TypeScript:

export let factory = () => {
    return class Foo {}
};

It fails to compile with the error

error TS4025: Exported variable ‘factory’ has or is using private name ‘Foo’.

Is there any way to allow TypeScript to return a class from a factory function?

Leave a Comment