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().
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.
@param ― callback A function that accepts up to three arguments. The flatMap method calls the
callback function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callback function. If
thisArg is omitted, undefined is used as the this value.
Combines two or more arrays.
This method returns a new array without modifying any existing arrays.
@param ― items Additional arrays and/or items to add to the end of the array.
concat(...
const array:number[][]
array);
Error ts(2769) ― No overload matches this call.
Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
Argument of type 'number[]' is not assignable to parameter of type 'ConcatArray<never>'.
The types returned by 'slice(...)' are incompatible between these types.
Type 'number[]' is not assignable to type 'never[]'.
Type 'number' is not assignable to type 'never'.
Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
Argument of type 'number[]' is not assignable to parameter of type 'ConcatArray<never>'.
The types returned by 'slice(...)' are incompatible between these types.
Type 'number[]' is not assignable to type 'never[]'.
Type 'number' is not assignable to type 'never'.
Calls the function with the specified object as the this value and the elements of specified array as the arguments.
@param ― thisArg The object to be used as the this object.
@param ― args An array of argument values to be passed to the function.
apply([], array);
Error ts(2345) ― Argument of type 'number[][]' is not assignable to parameter of type 'ConcatArray<never>[]'.
Type 'number[]' is not assignable to type 'ConcatArray<never>'.
The types returned by 'slice(...)' are incompatible between these types.
Type 'number[]' is not assignable to type 'never[]'.
Type 'number' is not assignable to type 'never'.
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.
@param ― callback A function that accepts up to three arguments. The flatMap method calls the
callback function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callback function. If
thisArg is omitted, undefined is used as the this value.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
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.
@param ― callback A function that accepts up to three arguments. The flatMap method calls the
callback function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callback function. If
thisArg is omitted, undefined is used as the this value.
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.