Skip to content

arrayMapIdentities

Reports using .flatMap() with an identity function that returns its argument unchanged.

✅ This rule is included in the ts logical and logicalStrict presets.

Using .flatMap() with a callback that returns its argument unchanged is equivalent to calling .flat(). The .flat() method is more concise and clearly expresses the intent to flatten an array.

const
const result: number[]
result
=
const values: number[][]
values
.
Array<number[]>.flatMap<number, undefined>(callback: (this: undefined, value: number[], index: number, array: number[][]) => number | readonly number[], thisArg?: undefined): number[]

Calls a defined callback function on each element of an array. Then, flattens the result into a new array. This is identical to a map followed by flat with depth 1.

@paramcallback A function that accepts up to three arguments. The flatMap method calls the callback function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callback function. If thisArg is omitted, undefined is used as the this value.

flatMap
((
value: number[]
value
) =>
value: number[]
value
);
const
const result: number[]
result
=
const values: number[][]
values
.
Array<number[]>.flatMap<number, undefined>(callback: (this: undefined, value: number[], index: number, array: number[][]) => number | readonly number[], thisArg?: undefined): number[]

Calls a defined callback function on each element of an array. Then, flattens the result into a new array. This is identical to a map followed by flat with depth 1.

@paramcallback A function that accepts up to three arguments. The flatMap method calls the callback function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callback function. If thisArg is omitted, undefined is used as the this value.

flatMap
((
item: number[]
item
) => {
return
item: number[]
item
;
});
const
const result: number[]
result
=
const values: number[][]
values
.
Array<number[]>.flatMap<number, undefined>(callback: (this: undefined, value: number[], index: number, array: number[][]) => number | readonly number[], thisArg?: undefined): number[]

Calls a defined callback function on each element of an array. Then, flattens the result into a new array. This is identical to a map followed by flat with depth 1.

@paramcallback A function that accepts up to three arguments. The flatMap method calls the callback function one time for each element in the array.

@paramthisArg An object to which the this keyword can refer in the callback function. If thisArg is omitted, undefined is used as the this value.

flatMap
(function (
element: number[]
element
) {
return
element: number[]
element
;
});

This rule is not configurable.

If you are maintaining a codebase that must support environments where .flat() is not available, you may need to disable this rule. Some older JavaScript environments do not support .flat() or .flatMap() and only polyfill .flatMap(), though this is rare in modern development.

Made with ❤️‍🔥 around the world by the Flint team and contributors.