Lets say I have the following method:
getErrorMessage(state: any, thingName?: string) {
const thing: string = state.path || thingName;
const messages: string[] = [];
if (state.errors) {
for (const errorName in state.errors) {
switch (errorName) {
case 'required':
messages.push(`You must enter a ${thing}`);
break;
case 'minlength':
messages.push(`A ${thing} must be at least ${state.errors['minlength'].requiredLength}characters`);
break;
case 'pattern':
messages.push(`The ${thing} contains illegal characters`);
break;
case 'validateCardNumberWithAlgo':
messages.push(`Card doesnt pass algo`);
break;
}
}
}
return messages;
}
when I run
ng lint
I get the following error :
for (... in ...) statements must be filtered with an if statement
Having a look at similar question I don't think that answer would be applicable to my situation. After all switch statement sits in the category of if-else-if ladder.
tslint should consider switch statement as form of if statement but it doesn't?!
if
statement should come after thefor...in
, and a switch statement can be seen as a kind ofif
statement, yet it doesn't resolve the error. – Traitorif (obj.hasOwnProperty(propName))
. Right now you cannot be sure thatrequired
exists directly onstate.errors
object or from a prototype of it. – Vengeif
statement and the error will go away - it doesn't check specifically forhasOwnProperty
. So in that case, aswitch
statement could be seen as similar to anyif
statement. Thecase
statements could even be targeting only the property names known not to be on the prototype. – Traitor