我正在将一个带有多个“生成的”原型(prototype)方法的 javascript 类转换为 typescript 。生成的方法被映射到一个内部对象,因此 API 对于 80% 的用例来说更干净/更方便。
但是,如果不实际实现它们,我无法正确地告诉 typescript 存在哪些方法。
class A {}
A.prototype.test = function() {}
var a = new A().test();
错误为 error TS2339: Property 'test' does not exist on type 'A'
我似乎可以通过手动定义属性来解决这个问题,但这阻碍了自动映射这些方法的实用性。
class A {
test: any;
}
A.prototype.test = function() {}
var a = new A();
a.test();
最佳答案
However, I'm find no way to properly tell typescript which methods exist without actually implementing them.
如果生成了方法,为什么还要定义类?也就是说,您实际上应该只声明类,而不是class A
,即您应该这样做:
declare class A {
test();
}
关于javascript - 向 typescript 声明动态原型(prototype)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38259133/