Reputation: 9884
I want to add new method catchAndLog
to existing Promise
class.
In plain JS, it's easy as Object.defineProperty(Promise.prototype, 'catchAndLog', ...)
. However, it seems typescript does not understand this.
How do I let typescript compiler know that Promise
now has an additional member property so that it doesn't complain when I do Promise.reject('error').catchAndLog()
?
Upvotes: 1
Views: 124
Reputation: 249586
You can do it in the same way as in Javascript, the only catch is you need to add a definition for the newly added methods so that typescript knows about it.
declare global { // Remove this enclosing global if you are not in a module
interface Promise<T> {
catchAndLog(): Promise<T>
}
}
Object.defineProperty(Promise.prototype, 'catchAndLog', {
})
Promise.reject('error').catchAndLog()
Upvotes: 2