Currently, I am in the process of writing unit tests using jasmine. During this process, I encountered an issue with the following code snippet:
let arg0: string = http.put.calls.argsFor(0) as string;
if(arg0.search(...)
This resulted in an error stating that arg0.search
is not a function. After investigating further, it became apparent that Intellisense was indicating that arg0 was actually an array instead of a string as expected. As a workaround, I decided to modify the code like so:
let arg0: string = http.put.calls.argsFor(0).toString();
if(arg0.search(...)
Surprisingly, this change resolved the issue. Upon closer inspection, I noticed that the argsFor function signature had "any" as the return type, which ultimately returned an array when called.
My question is why did the initial "as string" version fail to work, and why wasn't a compile error triggered since arg0 ended up storing an array rather than a string?