3

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.

2
  • Have you pulled in the jasmine.d.ts from the definitely typed repo? Commented Sep 22, 2015 at 21:26
  • Yes, I have the jasmine.d.ts referenced. Commented Sep 23, 2015 at 12:55

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I noticed you don't document how to acutally wire in a user-defined global.d.ts file. Can you please elaborate?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.