Search code examples
javascripteslintdestructuring

How to fix Eslint prefer-destructuring


How should I rewrite the following line to avoid getting this error

ctrl.formData.type = ctrl.types[0];

Solution

  • Tweaking the rule configuration based on the prefer-destructuring docs ought to avoid this case. This change would disable the rule for assignments like your example while keeping it for the more common case of declarations:

    "prefer-destructuring": ["error", {
      "AssignmentExpression": {
        "array": false,
        "object": false
      }
    }]
    

    If you want to keep the rule configured as-is, this one-liner works:

    [ctrl.formData.type] = ctrl.types;