Last time I learned how to make a typing script see methods copied into a class prototype from another place. All methods were reduced to the declaration of field types:

Fiddle

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 } } 

However, it now took one of the heirs to override this method:

 class OtherFinal extends Both { doSmth() { // Здесь ошибка console.log('doSmth from OtherFinal'); } } 

Class 'Both' defines an instance member property

Actually, the message is quite logical.
But how else can you make typescript see a method that is not explicitly stated in it?

All methods lead to the same problem (links lead to the corresponding fiddly):
doSmth: () => void , doSmth: typeof Second.prototype.doSmth; .

It is clear that you can declare the function doSmth() {} , but in this case the garbage gets into the compiled code, so I don’t want to do that.

PS: This question is in English.

1 answer 1

  1. It is necessary for Second to declare the interface .
  2. Both should be declared as an interface inheriting from First and Second .
  3. Implement Both as a constructor function with manual implementation of inheritance.
    Save to Both , declared as a constructor, returning Both .
    For cast use as any .

Here is the full code:

 class First { someMethod() { console.log('someMethod from First'); } } interface Second { constructor(): Second; doSmth(): void; } function Second() { console.log('Second'); } Second.prototype.doSmth = function () { console.log('doSmth from Second'); } interface Both extends First, Second { constructor(): Both; doBoth(): void; } var Both: { new (): Both } = function () { console.log('constructor of Both'); First.call(this); Second.call(this); } as any; Both.prototype = Object.create(First.prototype); Both.prototype.doBoth = function () { this.someMethod(); // ok this.doSmth(); // ok } 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() { // ok console.log('doSmth from OtherFinal'); } } 

PS: The solution is found when searching for the answer to another question.