Skip to content

arrayFlatMethods

Reports legacy techniques to flatten arrays instead of using .flat().

✅ This rule is included in the ts stylistic presets.

ES2019 introduced Array.prototype.flat() as the standard way to flatten arrays. .flat() is a more modern and concise way to flatten arrays compared to older techniques:

  • array.flatMap(value => value)
  • [].concat(...array)
  • [].concat.apply([], array)
  • Array.prototype.concat.apply([], array)
  • Array.prototype.concat.call([], ...array)

This rule reports legacy techniques that can be replaced with .flat().

declare const array: number[][];
array.flatMap((value) => value);
declare const array: number[][];
[].concat(...array);
declare const array: number[][];
[].concat.apply([], array);
declare const array: number[][];
Array.prototype.concat.apply([], array);
declare const array: number[][];
Array.prototype.concat.call([], ...array);
declare const array: number[][];
_.flatten(array);

This rule is not configurable.

If you need to support environments that don’t have Array.prototype.flat() (pre-ES2019), or if your codebase extensively uses lodash/underscore for array manipulation, this rule may not be for you. Alternately, if you have specific stylistic preferences that involve more verbose calls to granular APIs, you might prefer to disable this rule.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.