0

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.

2
  • The forEach method 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 call forEach on a variable that might contain either an array or an observable. Where are you getting this.data from that motivated you to give it that union type? Commented Sep 27, 2018 at 1:39
  • actually it comes from a node module package of ngx-formly/core and we are looping through templateoption of that . But i got solution of that. by doing type cast. like var result = data as any[]; Commented Sep 28, 2018 at 6:25

1 Answer 1

0

I got the solution by doing type cast as below

var result = this.data as any[]; result.forEach(function(d) { }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.