-
Notifications
You must be signed in to change notification settings - Fork 889
prefer-const: for-of loop with tuple assignment #2902
Comments
https://palantir.github.io/tslint/rules/prefer-const/ |
Then why is it set to |
// bad
let { foo, bar } = this.props;
// good
const { foo } = this.props;
let { bar } = this.props; I agree that in the case of a for-of loop, this kind of construct isn't really possible. So I suppose the rule could be more lenient here... but i'm not sure it's really worth the extra implementation complexity with this edge case. you can easily refactor your code to not mutate the I'm inclined to say this is "by design", but I'll leave this open for more feedback |
@adidahiya So basically. what you suggest is copying the values I really do need to mutate? My counter argument however would be that if you look at the for of page of mozilla's developers guide (https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Statements/for...of), they really do use the same example for iterating through a |
Well, yeah, they use the same syntax in the MDN example, but it's a super simple contrived example that you wouldn't use in a production app that has linting enabled: for (let [key, value] of iterable) {
console.log(value);
} Obviously the lint rule should complain about the |
It wouldn't be that hard to implement. I think it makes sense to handle destructuring in for-of-loop initializers in a more reasonable way. In fact we already have special handling for VariableDeclarationLists inside ForStatements when one variable is reassigned. for (let i = 0, len = arr.length; i < len; ++i) {} // outside of for-loops that would be a lint error |
@adidahiya Yeah, indeed it should complain there, that's why I mentioned it didn't alter any variable. |
ah, good point about the ForStatement special case. |
Bug Report
TypeScript code being linted
with
tslint.json
configuration:Actual behavior
It claims that the variable
key
is never reassigned even ifvalue
is.Expected behavior
In some cases I need to overwrite one of the tuple variables so I must use
let
in such case.So I expect tslint to not complain about some tuple variables not being reassigned if at least 2 variables are used within the tuple and at least one variable is being reassigned.
The text was updated successfully, but these errors were encountered: