I have a scenario where I need to add a new method to a prototype, specifically to a class created using TypeScript. Here is an example:
declare module "./MyClass" {
interface MyClass {
myNewMethod();
}
}
MyClass.prototype.myNewMethod = function() {
let foo = this; // TypeScript doesn't recognize 'foo' as being of type MyClass :-(
}
Unfortunately, TypeScript fails to identify the type of this
. Although I could manually declare foo
, I prefer not to introduce an extra variable just to work around this TypeScript limitation.
Is there a way to achieve this without creating additional variables?