简体   繁体   中英

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?

Quick answer

change this:

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

to that:

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

Longer answer

This error could be triggered/forced by to a tsconfig.json setting:

{
    "compilerOptions": {
        ...
        "declaration": true // this should be false or omitted

But that is not the reason, it is just a trigger. The real reason (as discussed here Error when exporting function that returns class: Exported variable has or is using private name ) comes from the Typescript compiler

when TS compiler founds statement like this

let factory = () => { ...

it must start to guess what is the return type, because that information is missing (check the : <returnType> placeholder) :

let factory = () : <returnType> => { ...

in our case, TS will quickly find out, that the returned type is easy to guess:

return class Foo {} // this is returned value, 
                    // that could be treated as a return type of the factory method

so, in case we would have that similar statement (this is not the same, as original statement at all, but let's just try to use it as an example to clarify what happens) we can properly declare return type:

export class Foo {} // Foo is exported
export let factory = () : Foo => { // it could be return type of export function
    return Foo
};

that approach would be working, because the Foo class is exported, ie visible to external world.

Back to our case. We want to return type which is not exported . And then, we MUST help TS compiler to decide, what is the return type.

It could be explicit any:

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

But even better would be to have some public interface

export interface IFoo {}

And then use such interface as return type :

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

I think this is a proper way to do it:

export let factory = () => {
     class Foo {/* ... */}
     return Foo as (new () => { [key in keyof Foo]: Foo[key] })
};

Check it out on playground

You need to export the class as well so that the consumer of the method can access the type.

Typically a factory will return an instance rather than a class or constructor.

UPDATED EXAMPLE

export class Foo {}; 

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

With ES6 you don't need to export the class type, but with TypeScript you absolutely should exporting the type signatures.

I was struggling with the same error. The solution for me was to remove the

"declaration": true

from tsconfig.json or to set it to false .

I've found separately provided solution satisfying:

export class MyClass {
   ...
}

export type MyClassRef = new (...args: any) => MyClass;

Given that signature I can use MyClassRef as a type of return value:

exposeMyClass(): MyClassRef {
    return MyClass;
}

Late answer on old question:

You need to have the class defined outside the factory method and define return value using 'typeof'.

class Foo {}

export const factory = (): typeof Foo => {
    return Foo;
};

Are you looking for returning Type of class from the function? Below is one code snippet how we can implement factory in TypeScript similar to in other languages.

    class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}
class Factory{
    getObject(msg:string): Greeter {
        return new Greeter(msg);
    }
}
var greeter = new Factory().getObject("Hi");
console.log(greeter.greet());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM