prefer-destructuring
Configuration
rslint.config.ts
Rule Details
Require destructuring from arrays and/or objects.
This rule extends the base ESLint prefer-destructuring rule with TypeScript-specific type awareness.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Type Annotation Handling
By default, the rule does not report on variable declarations with type annotations, because the auto-fix would remove them:
Type-Aware Array Detection
The rule uses the TypeScript type checker to determine whether numeric index access (x[0]) should be treated as array destructuring or object destructuring:
- If the object type is iterable (has
[Symbol.iterator]) orany, it is treated as array access - If the object type is a plain object with numeric keys (e.g.,
{ 0: unknown }), it is treated as object access
enforceForDeclarationWithTypeAnnotation
Examples of incorrect code with { "enforceForDeclarationWithTypeAnnotation": true }:
Examples of correct code with { "enforceForDeclarationWithTypeAnnotation": true }: