Skip to content

arrayFlatMethods

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

✅ This rule is included in the ts stylistic and stylisticStrict 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
const array: number[][]
array
: number[][];
const array: number[][]
array
.
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
);
declare const
const array: number[][]
array
: number[][];
[].
Array<never>.concat(...items: ConcatArray<never>[]): never[] (+1 overload)

Combines two or more arrays. This method returns a new array without modifying any existing arrays.

@paramitems Additional arrays and/or items to add to the end of the array.

concat
(...
const array: number[][]
array
);
declare const
const array: number[][]
array
: number[][];
[].
Array<never>.concat(...items: ConcatArray<never>[]): never[] (+1 overload)

Combines two or more arrays. This method returns a new array without modifying any existing arrays.

@paramitems Additional arrays and/or items to add to the end of the array.

concat
.
CallableFunction.apply<never[], ConcatArray<never>[], never[]>(this: (this: never[], ...args: ConcatArray<never>[]) => never[], thisArg: never[], args: ConcatArray<never>[]): never[] (+1 overload)

Calls the function with the specified object as the this value and the elements of specified array as the arguments.

@paramthisArg The object to be used as the this object.

@paramargs An array of argument values to be passed to the function.

apply
([],
const array: number[][]
array
);
declare const
const array: number[][]
array
: number[][];
var Array: ArrayConstructor
Array
.
ArrayConstructor.prototype: any[]
prototype
.
Array<any>.concat(...items: ConcatArray<any>[]): any[] (+1 overload)

Combines two or more arrays. This method returns a new array without modifying any existing arrays.

@paramitems Additional arrays and/or items to add to the end of the array.

concat
.
CallableFunction.apply<never[], number[][], any[]>(this: (this: never[], ...args: number[][]) => any[], thisArg: never[], args: number[][]): any[] (+1 overload)

Calls the function with the specified object as the this value and the elements of specified array as the arguments.

@paramthisArg The object to be used as the this object.

@paramargs An array of argument values to be passed to the function.

apply
([],
const array: number[][]
array
);
declare const
const array: number[][]
array
: number[][];
var Array: ArrayConstructor
Array
.
ArrayConstructor.prototype: any[]
prototype
.
Array<any>.concat(...items: ConcatArray<any>[]): any[] (+1 overload)

Combines two or more arrays. This method returns a new array without modifying any existing arrays.

@paramitems Additional arrays and/or items to add to the end of the array.

concat
.
CallableFunction.call<never[], number[][], any[]>(this: (this: never[], ...args: number[][]) => any[], thisArg: never[], ...args: number[][]): any[]

Calls the function with the specified object as the this value and the specified rest arguments as the arguments.

@paramthisArg The object to be used as the this object.

@paramargs Argument values to be passed to the function.

call
([], ...
const array: number[][]
array
);
declare const
const array: number[][]
array
: number[][];
const _: any
_
.
any
flatten
(
const array: number[][]
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 ❤️‍🔥 around the world by the Flint team and contributors.