Skip to content

arrayReduceTypeArguments

Reports Array#reduce calls using type assertions on initial values instead of type arguments.

✅ This rule is included in the ts logicalStrict presets.

When calling Array#reduce with an empty array or object as the initial value, TypeScript can’t infer useful types from those values. A common workaround is to use a type assertion like [] as number[] or {} as Record<string, boolean>. However, using the type parameter on reduce<T>() is preferred because it avoids type assertions and keeps the generic inference more consistent.

const
const result: number[]
result
= [1, 2, 3].
Array<number>.reduce<number[]>(callbackfn: (previousValue: number[], currentValue: number, currentIndex: number, array: number[]) => number[], initialValue: number[]): number[] (+2 overloads)

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

@paramcallbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

@paraminitialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

reduce
(
(
acc: number[]
acc
,
value: number
value
) =>
acc: number[]
acc
.
Array<number>.concat(...items: (number | ConcatArray<number>)[]): number[] (+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
(
value: number
value
* 2),
[] as number[],
);
const
const names: string[]
names
= ["a", "b", "c"];
const
const result: Record<string, boolean>
result
=
const names: string[]
names
.
Array<string>.reduce<Record<string, boolean>>(callbackfn: (previousValue: Record<string, boolean>, currentValue: string, currentIndex: number, array: string[]) => Record<string, boolean>, initialValue: Record<string, boolean>): Record<string, boolean> (+2 overloads)

Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

@paramcallbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.

@paraminitialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.

reduce
(
(
acc: Record<string, boolean>
acc
,
name: string
name
) => ({ ...
acc: Record<string, boolean>
acc
, [
name: string
name
]: true }),
{} as
type Record<K extends keyof any, T> = { [P in K]: T; }

Construct a type with a set of properties K of type T

Record
<string, boolean>,
);

This rule is not configurable.

Some codebases use exceedingly complex array reducers that are difficult to represent in the type system with type parameters. This rule can be difficult to enable on those projects. You might consider using Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.

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