Speedy response
modify this:
export let factory = () => {
return class Foo {}
};
to this:
export let factory = () : any => {
return class Foo {}
};
Detailed explanation
This issue could be caused by changing a specific configuration in tsconfig.json:
{
"compilerOptions": {
...
"declaration": true // should be false or omitted
However, that is just one factor. The main reason (as explained in this discussion Error when exporting function that returns class: Exported variable has or is using private name) originates from the Typescript compiler
when TS compiler encounters code like this
let factory = () => { ...
it needs to infer the return type without explicit information (see the : <returnType>
placeholder):
let factory = () : <returnType> => { ...
In this case, TS can easily determine the return type:
return class Foo {} // This serves as the inferred return type for the factory method
Therefore, if we had a similar statement (not identical to the original one but for illustrative purposes), we could specify the return type explicitly:
export class Foo {} // Foo is exported
export let factory = () : Foo => { // Declaring the return type of the export function
return Foo
};
This approach works because the Foo
class is exposed and accessible externally.
Returning to our scenario, the goal is to define the return type as something non-exportable, necessitating guidance for the TS compiler to make that determination.
An immediate solution could be to use any
as the explicit return type:
export let factory = () : any => {
return class Foo {}
};
Alternatively, defining a public interface would provide a more structured approach:
export interface IFoo {}
And subsequently utilizing this interface as the return type:
export let factory = () : IFoo => {
return class Foo implements IFoo {}
};