我正在努力定义如何编写 TypeScipt 代码,该代码表示该函数返回泛型类型的构造函数。有很多关于如何传递泛型类型的构造函数的例子,但没有关于如何返回的例子。
请检查以下示例:
这是抽象类的一部分:
getModel(): (new () => T) {
throw new Error('Method not implemented.'); // Error because don't know how to fix it
}
在派生类中,我试图像这样实现它:
getModel(): typeof User {
return User;
}
我有以下错误:
Type '() => typeof User' is not assignable to type '() => new () => User'.
如果我知道如何在抽象类中指定,我可以跳过派生类中的实现。
所以问题是 - 如何在抽象类级别指定该方法返回泛型类型的构造函数,并且我可以在子级别类中跳过此方法的实现?或者我在抽象类级别上没有正确指定返回签名?
编辑:
请检查奇怪的问题。A 类和 B 类的区别仅在于显式构造函数的存在。在 RealA 中不起作用,RealB 使用相同的 getModel() 方法。
class A {
a = '';
constructor(a: string) {
}
}
class B {
a = '';
static test(): void {
console.log('I do work');
}
}
abstract class Base<T> {
Prop: T;
constructor(TCreator: { new (): T; }) {
this.Prop = new TCreator();
}
getModel(): (new () => T) {
throw new Error('Method not implemented.'); // Error because don't know how to fix it
}
}
class RealA extends Base<A> {
getModel(): typeof A { // doesn't work - compilation error
return A;
}
}
class RealB extends Base<B> {
getModel(): typeof B { // works
return B;
}
}
var test = new RealA(A); // compile error
var test2 = new RealB(B)
对于 RealA 类同样的错误
() => typeof A' is not assignable to type '() => new () => A'