Essentially, I'm asking why this does not compile:
interface Constructor<T> {
new (...args: any[]): T;
}
function ConstructorFactory<T>(t: T) {
return t as any as Constructor<T>; // pretend we build a new class surrounding t somehow
}
class A extends ConstructorFactory({a: 11}) {
}
function AFactory<T>(t: T) {
return class AnonymousA extends ConstructorFactory(t) {
};
}
The error is the line with AnonymousA and says:
Base constructor return type 'T' is not a class or interface type.
The main thing I haven't been able to grasp is why I am able to define class A ok, but not AnonymousA. The generated javascript looks pretty similar for both, so I'm pretty sure this is either a bug, a subtle difference between anonymous classes and regular classes, or something I haven't learned about types. My bet is on the third option?
For context, this came from trying to use immutablejs records as classes. There is a lot of posts about this not working, but I haven't found anyone explain what I think is maybe a simpler example?
Also, I just wrote ConstructorFactory as an sketch quickly, I am aware that it's not actually returning a Constructor<T>. But I'm pretty sure typescript see's it as the right type.
Thanks for any help! Sorry if it's a duplicate question.