Home > database >  TSLint: forIn loop must be filtered
TSLint: forIn loop must be filtered

Time:10-07

I have an error with TSLINT on my project, about a forIn loop. The error is the next one:

for (... in ...) statements must be filtered with an if statement

I have tried both solutions Object.prototype.hasOwnProerty.call(object, key) and object.hasOwnproperty(key), but I still have the error.

Here is the code:

const data = result.data
for (const key in data) {
  // Must use a if statement
  if (data.hasOwnProperty(key)) {
    // code...
  }
}

Do you have any idea about what I am missing ?

Thanks all

CodePudding user response:

You can disable this by adding configuration in lint file.

reference : https://palantir.github.io/tslint/rules/forin/

OR Second option: Try this:

const data = result.data;
let key;
for (key in data) {
  if (data.hasOwnProperty(key)) {
    // code...
  }
}```


CodePudding user response:

My bad,

Actually, there was a single line outside the if statement handlebars, which uses the key...

NB for myself: make sure to check the statement scope.

  • Related