Skip to content
← Back to rules

eslint/prefer-destructuring Style

🛠️ An auto-fix is available for this rule for some violations.

What it does

Require destructuring from arrays and/or objects.

Why is this bad?

With JavaScript ES2015, a new syntax was added for creating variables from an array index or object property, called destructuring. This rule enforces usage of destructuring instead of accessing a property through a member expression.

Examples

Examples of incorrect code for this rule:

js
// With `array` enabled
const foo = array[0];
bar.baz = array[0];
// With `object` enabled
const qux = object.qux;
const quux = object["quux"];

Examples of correct code for this rule:

js
// With `array` enabled
const [foo] = array;
const arr = array[someIndex];
[bar.baz] = array;

// With `object` enabled
const { baz } = object;
const obj = object.bar;

Configuration

The 1st option

This option is an object with the following properties:

array

type: boolean

object

type: boolean

The 2nd option

This option is an object with the following properties:

enforceForRenamedProperties

type: boolean

default: false

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "prefer-destructuring": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "prefer-destructuring": "error",
  },
});
bash
oxlint --deny prefer-destructuring

Version

This rule was added in v1.10.0.

References