0

I am running this code:

const list = [1, 2, 3];
const flag1 = false;
const flag2 = true;

let selection = list[0]; // optimistic

if (flag1) {
  selection = list[1];
} else if (flag2) {
  selection = list[2];
}

and my ESLint is throwing this warning:

Use array destructuring. eslint(prefer-destructuring)

How can I solve this warning in a conditional assignment?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Raul
  • 2,673
  • 1
  • 15
  • 52
  • The message says how: "use array destructuring". There are examples on the rule page, too: https://eslint.org/docs/rules/prefer-destructuring. – jonrsharpe May 27 '22 at 08:12
  • 2
    This isn't a great place to use destructuring though, it seems better to override the rule. – pilchard May 27 '22 at 08:15
  • @pilchard this is why I am asking. I would never use destructuring in a similar scenario (conditional assignation) – Raul May 27 '22 at 08:17
  • 1
    Just disable the rule for the line `// eslint-disable-next-line prefer-destructuring`. see: [Turning off eslint rule for a specific line](https://stackoverflow.com/questions/27732209/turning-off-eslint-rule-for-a-specific-line) – pilchard May 27 '22 at 08:17

0 Answers0