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.
Examples
Section titled “Examples”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);declare const array: number[];array.flatMap((value) => [value, value * 2]);declare const strings: string[];strings.flatMap((value) => value.split(","));declare const array: number[];array.map((value) => [[value]]).flat(2);declare const array: number[];array.flat();Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useFlatMap - ESLint:
unicorn/prefer-array-flat-map - Oxlint:
unicorn/prefer-array-flat-map
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.