I have this function in my TS project:
size(): number {
let size = 0;
for (const _ of this) {
size++;
}
return size;
}
While I want to continue using a for each to count the elements I get following eslint error:
'_' is assigned a value but never used
In other languages like ruby, prefixing with an underscore would fix this. In this case that doesn't work.
You can configure your ESLint no-unused-vars
rule to add a varsIgnorePattern
, e.g. ^_
for “starts with underscore”:
"no-unused-vars": ["error", {
"varsIgnorePattern": "^_"
}]
Alternatively, you can suppress the error on the code side, e.g. with the void
operator (an idiomatic way of discarding the result of any expression) as @JaromandaX commented:
size(): number {
let size = 0;
for (const _ of this) {
void _;
size++;
}
return size;
}
ESLint also has a general mechanism for inline rule configuration.