reduceTypeParameters
Reports
Array#reducecalls using type assertions on initial values instead of type parameters.
✅ 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.
Examples
Section titled “Examples”const result = [1, 2, 3].reduce( (acc, value) => acc.concat(value * 2), [] as number[],);const names = ["a", "b", "c"];const result = names.reduce( (acc, name) => ({ ...acc, [name]: true }), {} as Record<string, boolean>,);const result = [1, 2, 3].reduce<number[]>( (acc, value) => acc.concat(value * 2), [],);const names = ["a", "b", "c"];const result = names.reduce<Record<string, boolean>>( (acc, name) => ({ ...acc, [name]: true }), {},);const sum = [1, 2, 3].reduce((acc, value) => acc + value, 0);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.