I have a piece of code where I have data variable with given declaration. But when I loop through data object I am getting error as below:
"cannot invoke an expression whose type lacks a call signature"
export class AppComponent { title = 'app works!'; data : any[] | Observable<any[]>; constructor() { this.data.forEach(function(d) { }); } } Not sure it is typescript issue or due to incorrect type declaration.
forEachmethod means something very different for an array and an observable (re observable, see this answer found in a web search), so it doesn't make much sense to callforEachon a variable that might contain either an array or an observable. Where are you gettingthis.datafrom that motivated you to give it that union type?