我在 Angular.js 项目上使用 TypeScript 并使用 Jasmine 进行测试。
当我使用 spyOn
模拟对象上的方法时,Jasmine 会用具有 calls
属性的函数替换该方法,这样您就可以执行,例如,thing.method.calls.count()
.
问题是 TypeScript 编译器不知道方法上的 calls
属性并给出编译器错误:
property 'calls' does not exist on type '() => IPromise<IReport[]>'
如何修复此错误?我是否需要定义一个具有函数签名和对象属性的新接口(interface)?我尝试过使用不同的界面配置,但到目前为止运气不佳。
最佳答案
How do I fix this error?
基本上在globals.d.ts
这样的文件中添加到Function
接口(interface)。演示:
interface Function {
calls: any;
}
var foo = ()=>null;
foo.calls; // okay
这里介绍了这个技巧:https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html
关于angularjs - 具有属性的函数的 TypeScript 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32726515/