oxc-project/oxc · warning · OxcDiagnostic
Use Object destructuring.
Error message
Use Object destructuring.
What it means
Diagnostic from the oxlint `prefer-destructuring` rule for object targets. It fires when code reads a property into a variable via member access (`const foo = object.foo;`) and the rule's object option is enabled, suggesting `const { foo } = object;` instead (prefer_destructuring.rs:21-25). The rule config (PreferDestructuringTargetConfig in the same file) gates this with `{ "object": true }` or "always".
Source
Thrown at crates/oxc_linter/src/rules/eslint/prefer_destructuring.rs:21
ast::{
AssignmentTarget, BindingPattern, Expression, MemberExpression, VariableDeclarationKind,
},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
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 {View on GitHub (pinned to e1e7af627c)
Solutions
- Use object destructuring: `const foo = object.foo;` becomes `const { foo } = object;` (auto-fixable when names match).
- If names must differ, destructure with a rename: `const { foo: bar } = object;`.
- Turn the option off (`{ "object": false }`) if direct member access is your style.
- Inline-disable for cases where destructuring hurts clarity (chained access like `const v = obj.a.b.c;`).
Example fix
// before
const foo = object.foo;
// after
const { foo } = object; Defensive patterns
Strategy: validation
Validate before calling
// .oxlintrc.json — many teams enable object only
{ "rules": { "prefer-destructuring": ["warn", { "object": true, "array": false }] } } Prevention
- Destructure at the point of member-access reads (`const { foo } = obj;`).
- Use rename syntax when variable and key names differ.
- Auto-fix matching-name cases with the linter rather than by hand.
When it happens
Trigger: Enable `"prefer-destructuring": ["error", { "object": true }]` (or "always") and write `const version = config.version;` or `this.name = user.name;` where a matching-key destructure is possible.
Common situations: Configs from teams standardizing on destructuring for imports/config objects; React props extraction (`const title = props.title;`); assignments from deeply nested options; cases where the variable name differs from the key — those need renaming or a default (`const { title: t } = props`) and are only fixable when names match.
Related errors
- Use Array destructuring.
- Unexpected function expression.
- Expected longform method syntax.
- Empty array binding pattern
- Empty object binding pattern
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/de41d7afa29fc5ad.
Report an issue: GitHub.