Destructuring two variables and reassigning the value of one is hard with let and const
See original GitHub issueTell us about your environment
- ESLint Version: 3.16.1
- Node Version: v6.6.0
- npm Version: 3.10.3
What parser (default, Babel-ESLint, etc.) are you using? default
Please show your full configuration:
module.exports = {
"env": {
"browser": true,
"es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
},
"rules": {
"prefer-const": ["error", {
"destructuring": "any",
"ignoreReadBeforeAssign": false
}],
"no-console": "off"
}
};
What did you do? Please include the actual source code causing the issue.
I have a basic test case of the problem in this code:
const props = {
name: "Eric",
age: 30,
occupation: "Artist"
}
let { name, age } = props;
age = 3;
console.log(name);
console.log(age);
ESLint says that let { name, age } = props;
is a problem and should use const
.
Here, two variables are created via destructuring syntax, and one is later reassigned but the other is not.
Fixing the code as the rule suggests means using const
, which creates a syntax error.
What did you expect to happen?
I’m not sure 😃
What actually happened? Please include the actual, raw output from ESLint.
$ ./node_modules/.bin/eslint app.js
/Users/eric/Desktop/eslint/app.js
7:7 error 'name' is never reassigned. Use 'const' instead prefer-const
✖ 1 problem (1 error, 0 warnings)
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Destructuring assignment - JavaScript - MDN Web Docs
The destructuring assignment syntax is a JavaScript expression that ... Two variables values can be swapped in one destructuring expression.
Read more >Does a Javascript Object Destructure assignment use const ...
Declaring variables with destructuring uses the same scope as if you'd declared ordinary variables in the same position. So if you do: let...
Read more >Destructuring • JavaScript for impatient programmers (ES2022 ...
Here, we have two default values that are assigned to the variables x and y because the corresponding elements don't exist in the...
Read more >JavaScript Object Destructuring, Spread Syntax, and the Rest ...
JavaScript Object Destructuring is the syntax for extracting values from an object property and assigning them to a variable.
Read more >Destructuring in JavaScript: the not so good parts
Provide default values ... 3// you know have age value 30 in the variable userAge ... 2let [one, two, three] = new Set([1,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
🎊
Does the
destructuring: "all"
option solve your issue?