0
我想將某些方法混合到一個抽象基類中來創建一個新的抽象類。在Typescript中混入抽象基類中
看看下面的例子:
abstract class Base {
abstract method();
}
interface Feature {
featureMethod();
}
class Implementation extends Base implements Feature {
method() {
}
featureMethod() {
// re-usable code that uses method() call
this.method();
}
}
這工作得很好,但我們的目標是走型接口的實現,並將其移動到一個mixin,因此它可以被重新使用的其他實現基類。
我有以下內容,但它並沒有在打字稿2.4.1
type BaseConstructor<T = Base > = new (...args: any[]) => T;
export function MixFeature<BaseType extends BaseConstructor>(TheBase: BaseType) {
abstract class Mixed extends TheBase implements Feature {
featureMethod() {
// re-usable code that uses method() call
this.method();
}
}
return Mixed;
}
class Implementation extends MixFeature(Base) {
method() {
}
}
編譯,但打字稿不同意,他說:
Error:(59, 41) TS2345:Argument of type 'typeof Base' is not assignable to parameter of type 'BaseConstructor<Base>'.
Cannot assign an abstract constructor type to a non-abstract constructor type.
是否有可能使這個工作,還是它是一個Typescript限制,抽象的基礎不能使用mixins擴展?