Skip to content

arrayFlatMapMethods

Reports using .map().flat() when .flatMap() can be used.

✅ This rule is included in the ts stylistic presets.

Array.prototype.flatMap() combines mapping and flattening in a single step. Using .map().flat() creates an intermediate array that is immediately discarded, which is less efficient. This rule reports when .map().flat() can be replaced with .flatMap().

The rule only applies when .flat() is called with no arguments or with a depth of 1, since .flatMap() always flattens to a depth of one level.

declare const array: number[];
array.map((value) => [value, value * 2]).flat();
declare const strings: string[];
strings.map((value) => value.split(",")).flat();
declare const array: number[];
array.map((value) => [value]).flat(1);

This rule is not configurable.

If you prefer the explicit two-step approach of .map().flat() for readability, or if your codebase has a large number of existing uses that would be difficult to refactor, you may disable this rule.

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