no-useless-destructuring

January 28, 2019 ยท View on GitHub

Detects array and object destructuring that doesn't assign to a variable.s

Rationale

Destructuring can become very hard to read, especially when nesting comes into play. Sometimes a refactoring leaves a destructuring behind that no longer assigns a value and is therefore useless. This rule suggests to either remove or simplify certain redundant destructuring patterns.

Examples

:thumbsdown: Examples of incorrect code

declare let obj: Record<string, any>;
declare let arr: any[];

// the following statements don't assign a variable
let {} = obj;
({} = obj);
({prop: {}} = obj);
let [] = arr;
[] = arr;
[, {}, []] = arr;

// the following could be simplified
let [first, , , ] = arr;
let [{}, ...[, ...rest]] = arr;

:thumbsup: Examples of correct code

declare let obj: Record<string, any>;
declare let arr: any[];

let {...clone} = obj;
let {a: {}, ...subset} = obj; // empty destructuring of 'a' is used to exclude that property
({b: {}, ...subset} = obj);

let [...clonedArr] = arr;
let [, , ...rest] = arr;
let [first] = arr;

// empty destructuring can be used to avoid giving unused parameters a name (only works for non-nullable parameters)
function fn({}: string, []: string[], param: boolean) {}

Further Reading: