--- description: 'Require destructuring from arrays and/or objects.' --- import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; > 🛑 This file is source code, not the primary documentation location! 🛑 > > See **https://typescript-eslint.io/rules/prefer-destructuring** for documentation. It adds support for TypeScript's type annotations in variable declarations. ## Examples ```ts const x: string = obj.x; // This is incorrect and the auto fixer provides following untyped fix. // const { x } = obj; ``` ```ts const x: string = obj.x; // This is correct by default. You can also forbid this by an option. ``` And it infers binding patterns more accurately thanks to the type checker. ```ts const x = ['a']; const y = x[0]; ``` ```ts const x = { 0: 'a' }; const y = x[0]; ``` It is correct when `enforceForRenamedProperties` is not true. Valid destructuring syntax is renamed style like `{ 0: y } = x` rather than `[y] = x` because `x` is not iterable. ## Options This rule adds the following options: ```ts type Options = [ BasePreferDestructuringOptions[0], BasePreferDestructuringOptions[1] & { enforceForDeclarationWithTypeAnnotation?: boolean; }, ]; const defaultOptions: Options = [ basePreferDestructuringDefaultOptions[0], { ...basePreferDestructuringDefaultOptions[1], enforceForDeclarationWithTypeAnnotation: false, }, ]; ``` ### `enforceForDeclarationWithTypeAnnotation` {/* insert option description */} Examples with `{ enforceForDeclarationWithTypeAnnotation: true }`: ```ts option='{ "object": true }, { "enforceForDeclarationWithTypeAnnotation": true }' const x: string = obj.x; ``` ```ts option='{ "object": true }, { "enforceForDeclarationWithTypeAnnotation": true }' const { x }: { x: string } = obj; ```