question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Destructuring two variables and reassigning the value of one is hard with let and const

See original GitHub issue

Tell 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ericandrewlewiscommented, Mar 30, 2017

🎊

2reactions
not-an-aardvarkcommented, Mar 2, 2017

Does the destructuring: "all" option solve your issue?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found