oxc-project/oxc · warning · OxcDiagnostic

Use Array destructuring.

Error message

Use Array destructuring.

What it means

Diagnostic from the oxlint `prefer-destructuring` rule for array targets. It fires when code indexes into an array to pull values (`const foo = array[0];`) and the rule's array option is enabled, suggesting `const [foo] = array;` (prefer_destructuring.rs:27-31). Enabled via `{ "array": true }` or the "always" shorthand in .oxlintrc.json.

Source

Thrown at crates/oxc_linter/src/rules/eslint/prefer_destructuring.rs:27

use oxc_span::{GetSpan, Span};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    AstNode,
    ast_util::variable_declaration_kind,
    context::LintContext,
    rule::{Rule, TupleRuleConfig},
};

fn prefer_object_destructuring(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use Object destructuring.")
        .with_help("Use object destructuring rather than direct member access.")
        .with_label(span)
}

fn prefer_array_destructuring(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use Array destructuring.")
        .with_help("Use array destructuring rather than direct member access.")
        .with_label(span)
}

#[derive(Debug, Clone, JsonSchema, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
struct PreferDestructuringTargetConfig {
    array: bool,
    object: bool,
}

impl Default for PreferDestructuringTargetConfig {
    fn default() -> Self {
        Self { array: true, object: true }
    }
}

impl PreferDestructuringTargetConfig {

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Destructure the index: `const foo = array[0];` becomes `const [foo] = array;`; multi-index reads become `const [x, y] = point;`.
  2. Disable the array half of the rule: `["error", { "array": false, "object": true }]`.
  3. Use rest/spread when taking head and tail: `const [head, ...rest] = items;`.
  4. Inline-disable for readability exceptions like `arr[arr.length - 1]`.

Example fix

// before
const foo = array[0];

// after
const [foo] = array;
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — opt out of array half if single-index reads annoy the team
{ "rules": { "prefer-destructuring": ["warn", { "object": true, "array": false }] } }

Prevention

When it happens

Trigger: Enable array destructuring reporting and write `const first = items[0];` or `this.x = point[0]; this.y = point[1];` (the swap-style case becomes `[this.x, this.y] = point;`).

Common situations: Tuple return values (e.g. `const m = match(...); const name = m[1];` for regex captures); coordinate/pair handling; teams that enable array destructuring for looks but dislike it for single-index reads (commonly disabled with `{ "array": false, "object": true }`).

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/7054973d9efe1498. Report an issue: GitHub.