【发布时间】:2018-12-02 01:08:53
【问题描述】:
我正在尝试扩展第 3 方课程,但无法让 typescript 发挥出色。基本上,我不能在我的新方法中使用类中已经定义的任何现有方法。
一种解决方法是重新定义extensions.ts 中的现有方法(见下文),但必须有更好的方法。
第三者index.d.ts
export as namespace thirdParty;
export Class SomeClass {
// some methods here
}
我的extensions.ts
import {thirdParty} from 'thirdParty'
declare module 'thirdParty' {
namespace thirdParty {
class SomeClass{
newMethod(): this
// works if I redfine the method here
originalExistingMethod(): number
}
}
}
thirdParty.SomeClass.prototype.newMethod = function() {
return this.originalExistingMethod() + 1
}
当调用像上面的this.originalExistingMethod() 这样的现有方法时,打字稿会抱怨:
TS2339: Property 'originalExistingMethod' does not exist on type 'SomeClass'
有没有办法避免在执行module augmentation时重新定义现有方法?
【问题讨论】:
-
真正的问题是:你为什么要修改第三方类?温和地说,这是一种不好的做法。因为这是一种不好的做法,您将很难为此找到好的/正确的解决方案。
标签: javascript typescript