Inheritance 使用重写模拟多重继承

Inheritance 使用重写模拟多重继承,inheritance,typescript,multiple-inheritance,mixins,overriding,Inheritance,Typescript,Multiple Inheritance,Mixins,Overriding,我发现了如何强制typescript查看从另一个地方复制到类原型的方法。方法是声明字段: 但是现在我需要在一个子类中重写这样的方法: class OtherFinal extends Both { doSmth() { // Here is an error console.log('doSmth from OtherFinal'); } } 类“Both”定义实例成员属性“doSmth”,但类“OtherFinal”将其定义为实例成员函数 这条消息绝对合乎逻

我发现了如何强制typescript查看从另一个地方复制到类原型的方法。方法是声明字段:

但是现在我需要在一个子类中重写这样的方法:

class OtherFinal extends Both {
    doSmth() { // Here is an error
        console.log('doSmth from OtherFinal');
    }
}
类“Both”定义实例成员属性“doSmth”,但类“OtherFinal”将其定义为实例成员函数

这条消息绝对合乎逻辑。
是否有其他方法可以使typescript看到未直接实现的方法

我知道的所有方法都会导致相同的问题(链接会导致相应的小提琴):

我知道我可以声明一个函数 ,但在这种情况下,垃圾会进入编译代码,所以我不想这样做


PS:

您可以通过将
其他final
类更改为使用方法属性
doSmth
而不是方法来解决此错误:

class OtherFinal extends Both {
    doSmth = () => { // notice the change
        console.log('doSmth from OtherFinal');
    }
}

请记住,它会将
doSmth
绑定到创建的
OtherFinal

实例,这是一种不好的方法,因为它会将
doSmth
函数放入每个实例而不是原型中:在原型上手动设置方法是否适合您的用例
B.prototype.doSmth=function(){console.log('doSmth from OtherFinal');}
这是一种可能的方法。但是在这种情况下,你必须将复制的方法和非复制的方法分开,当你从一个基类得到它们时,这很奇怪。你知道,我认为从两个类继承是一个非常非常糟糕的主意。使用合成和接口代替!至于你的问题,我认为没有什么好办法。
class OtherFinal extends Both {
    doSmth = () => { // notice the change
        console.log('doSmth from OtherFinal');
    }
}