I'm facing a challenge while trying to extend a third-party class in TypeScript. The issue is that I am unable to access any existing methods of the class within my new method.
One possible solution could be to redeclare the existing methods in a separate file called extensions.ts
(as shown below), but I believe there must be a more efficient approach.
Third-Party index.d.ts
export as namespace thirdParty;
export Class SomeClass {
// various methods here
}
My Custom extensions.ts
import {thirdParty} from 'thirdParty'
declare module 'thirdParty' {
namespace thirdParty {
class SomeClass{
newCustomMethod(): this
// only functional if I redefine the method here
originalExistingMethod(): number
}
}
}
thirdParty.SomeClass.prototype.newCustomMethod = function() {
return this.originalExistingMethod() + 1
}
But, when attempting to use an existing method such as this.originalExistingMethod()
within the custom function above, TypeScript throws an error:
TS2339: Property 'originalExistingMethod' does not exist on type 'SomeClass'
Is there a way to avoid having to redeclare existing methods during module augmentation?