【发布时间】:2017-10-06 16:41:41
【问题描述】:
我想在抽象基类中混入一些方法,创建一个新的抽象类。
举个例子:
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();
}
}
这很好用,但目标是获取 Feature 接口的实现并将其移动到一个 mixin 中,以便基类的其他实现可以重用它。
我得到了以下内容,但它不能在 Typescript 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() {
}
}
但是Typescript不赞成,说:
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 扩展抽象基础?
【问题讨论】:
标签: typescript abstract mixins