Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript interface for function with properties

I'm using TypeScript on an Angular.js project and testing with Jasmine.

When I mock a method on an object using spyOn, Jasmine replaces the method with a function that has a calls property so you can do, for instance, thing.method.calls.count().

The problem is that the TypeScript compiler doesn't know about the calls property on the method and gives a compiler error:

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

How do I fix this error? Do I need to define a new interface that has a function signature as well as an object property? I've tried monkeying around with different interface configurations, but without luck so far.

like image 749
exupero Avatar asked Feb 17 '26 03:02

exupero


1 Answers

How do I fix this error?

Basically add to the Function interface in a file like globals.d.ts. Demo:

interface Function {
    calls: any;
}

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

This trick is covered here : https://basarat.gitbooks.io/typescript/content/docs/types/lib.d.ts.html

like image 200
basarat Avatar answered Feb 19 '26 01:02

basarat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!