具有属性的函数的 TypeScript 接口

TypeScript interface for function with properties

我在 Angular.js 项目上使用 TypeScript 并使用 Jasmine 进行测试。

当我使用 spyOn 模拟对象上的方法时,Jasmine 将方法替换为具有 calls 属性 的函数,因此您可以执行 thing.method.calls.count().

问题是 TypeScript 编译器不知道方法上的 calls 属性 并给出编译器错误:

property 'calls' does not exist on type '() => IPromise<IReport[]>'

如何修复此错误?我是否需要定义一个具有函数签名和对象 属性 的新接口?我尝试过使用不同的界面配置,但到目前为止运气不佳。

How do I fix this error?

基本上在globals.d.ts这样的文件中添加到Function界面。演示:

interface Function {
    calls: any;
}

var foo = ()=>null;
foo.calls; // okay 

这里介绍了这个技巧:https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html