typescript 3.1.2
I have searched through many stack overflow questions and online articles, and I've seen the same types of answers many times to questions about modifying existing typescript classes such as String
and Array<T>
, but I can't seem to get this working for the Promise<T>
class.
I've already read through all of these, no luck:
How to define global function in TypeScript?
How to add file with extending prototype in Typescript
How to extend String Prototype and use it next, in Typescript?
Cypress Custom TypeScript Command is not a Function
Here's my current code (I've tried many variations):
Promise.d.ts
declare global {
export interface Promise<T> {
catchWrapper(): Promise<T>;
}
}
Promise.ts
Promise.prototype.catchWrapper = function<T>(this: Promise<T>): Promise<T> {
return Promise.prototype.catch.apply(this, [e => {
console.log(`catch wrapper. ${e}`);
}]);
}
(I've tried adding export { }
in Promise.ts, it doesn't help)
another.ts
import '../theDir/Promise'
anAsyncMethod().catchWrapper();
This all compiles but I continually get runtime errors:
UnhandledPromiseRejectionWarning: TypeError: anAsyncMethod().catchWrapper is not a function
Is my implementation of catchWrapper() not being matched up to the declaration of the interface by the compiler?
Any ideas on how to fix this?