【发布时间】:2018-12-28 07:20:37
【问题描述】:
打字稿 3.1.2
我搜索了很多堆栈溢出问题和在线文章,对于修改现有打字稿类的问题,例如String和Array<T>,我多次看到相同类型的答案,但我似乎无法让它为Promise<T> 类工作。
我已经阅读了所有这些,没有运气:
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
这是我当前的代码(我尝试了很多变体):
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}`);
}]);
}
(我尝试在 Promise.ts 中添加 export { },但没有帮助)
另一个.ts
import '../theDir/Promise'
anAsyncMethod().catchWrapper();
这一切都可以编译,但我不断收到运行时错误:
UnhandledPromiseRejectionWarning: TypeError: anAsyncMethod().catchWrapper is not a function
我的 catchWrapper() 实现是否与编译器的接口声明不匹配?
关于如何解决这个问题的任何想法?
【问题讨论】:
标签: typescript