我希望有一个Typescript Mixin,其中包含一个抽象方法,并由混合类实现。类似于以下内容。
class MyBase {
}
type Constructor<T = {}> = new (...args: any[]) => T;
function Mixin<TBase extends Constructor<MyBase>>(Base: TBase) {
return class extends Base {
baseFunc(s: string) {};
doA()
{
this.baseFunc("A");
}
}
};
class Foo extends Mixin(MyBase) {
constructor()
{
super();
}
baseFunc(s: string)
{
document.write("Foo "+ s +"... ")
}
};
现在,这个可以工作,但我真的希望将mixin中的baseFunc变为抽象方法以确保在Foo类中实现。但有什么方法可以实现这一点吗?因为abstract baseFunc(s:string);
要求使用抽象类,而mixin中不允许使用抽象类...
return abstract class...
不起作用?(编辑 - 啊!因为它是匿名的!) - Roddyfunction Mixin<B extends Constructor<{}>>(Base: B)
进行修复。 - Joe Lapp