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.
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.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue 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.
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.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue 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<Kextendskeyofany, T> = { [PinK]:T; }
Construct a type with a set of properties K of type T
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.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue 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.
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.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue 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<
type Record<Kextendskeyofany, T> = { [PinK]:T; }
Construct a type with a set of properties K of type T
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.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue 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.
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.