I would like to mix in some methods to an abstract base class, to make a new abstract class.
Take the following example:
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();
}
}
This works fine, but the goal is to take the implementations of the Feature interface and move it to a mixin so it can be re-used by other implementations of the Base class.
I have got to the following, but it doesn't compile in 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() {
}
}
But Typescript does not approve, saying:
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.
Is it possible to make this work or is it a Typescript limitation that abstract bases cannot be extended using mixins?