Last time我发现如何强制打印脚本来查看从其他地方复制到类原型的方法。该方法是有关声明字段:用重写模仿多重继承
class First {
someMethod() {
console.log('someMethod from First');
}
}
function Second() {
console.log('Second');
}
Second.prototype.doSmth = function() {
console.log('doSmth from Second');
}
class Both extends First {
constructor() {
console.log('constructor of Both');
super();
Second.call(this);
}
doSmth:() => void
}
for (let key in Second.prototype) {
Both.prototype[key] = Second.prototype[key];
}
class Final extends Both {
doIt() {
this.someMethod();
this.doSmth();
Both.prototype.doSmth(); // ok
Final.prototype.doSmth(); // ok
}
}
但现在我需要在子类中的一个重写这样的方法:
class OtherFinal extends Both {
doSmth() { // Here is an error
console.log('doSmth from OtherFinal');
}
}
类“这两个”定义实例成员属性'doSmth',但'OtherFinal'类将其定义为实例成员函数。
该消息是绝对符合逻辑的。
是否有其他方法让打字稿看到没有直接实施的方法?
我知道导致同样的问题的所有方面(链接导致相应的小提琴):
doSmth:() => void
,doSmth: typeof Second.prototype.doSmth;
。
据我所知,我可以声明一个函数 doSmth() {}
,但在这种情况下,垃圾将进入编译代码,所以我不想这样去。
你知道,我认为从两个类继承是一个非常糟糕的主意。改为使用组合和接口! – Louy
至于你的问题,我认为没有*整洁的解决方案。 – Louy