If you want to give a name to the type of the return value of makeMyClass()
which is visible outside of the function, then you have to describe its structure as a type of interface in an outer scope. At least that's how I understand TypeScript's implementation of class expressions.
Here's how I would do it in your case. A class has an instance type (which describes instance properties and class methods) and a constructor type (which describes the constructor and any static methods), so we need to describe both:
export interface MyClassInstance {
foo(): void;
bar(): void;
// define the rest of the public interface here
}
export interface MyClassConstructor {
new(): MyClassInstance;
readonly prototype: MyClassInstance;
// define any static methods here
}
Now you can declare that makeMyClass()
returns the constructor type:
export const makeMyClass = function(a: string, b: boolean): MyClassConstructor {
// implement the MyClassInstance/MyClassConstructor interfaces
class MyClass {
foo() {
// ...
}
bar() {
// ...
}
}
return MyClass;
}
Note that this is repetitive, in that you declare the class structure both inside and outside the function. If you add a property or method to the interfaces, you will need to add a corresponding property to the class implementation. This seems to be unfortunately unavoidable, since you can't export types from inside functions.
Anyway, hope that helps; good luck!