Dailycode.info

Short solution for short problems

tslint: for .. in .. gives error: (... in ...) statements must be filtered with an if statement (forin)

I was rewriting a pipe from angular 1 to 4. There was a statement where tslint was giving me this error:


The code looked like this:
for (i in o) {
r.push(o[i]);
}

To get rid of the warning from tslint, you have to write an if inside the for loop to check if the proprty really exitst:

for (i in o) {
if (o.hasOwnProperty(i)) {
r.push(o[i]);
}
}


Because the object o is not an array, it expects the if statement to be present after the for loop. A switch statement cannot be used either without tslint warnings.



blog comments powered by Disqus