Skip to content

fix(lib): reorder Array#reduce/reduceRight overloads in lib.d.ts - #63064

Open
taronsung wants to merge 4 commits into
microsoft:mainfrom
taronsung:fix/reorder-array-reduce-overloads
Open

fix(lib): reorder Array#reduce/reduceRight overloads in lib.d.ts#63064
taronsung wants to merge 4 commits into
microsoft:mainfrom
taronsung:fix/reorder-array-reduce-overloads

Conversation

@taronsung

Copy link
Copy Markdown

Summary

Reorder the overload declarations for reduce and reduceRight in both ReadonlyArray<T> and Array<T> interfaces so the generic <U> overload (with initialValue: U) is checked before the same-type overload (with initialValue: T).

Problem

When calling reduce with an initial value of a different type than the array elements, TypeScript incorrectly infers the return type. This is because TypeScript checks overloads in declaration order, and the initialValue: T overload matches before the generic <U> overload gets a chance.

Example from #7014:

typeUnaryFunction=(arg1: any)=>any;typeBinaryFunction=(arg1: any,arg2: any)=>any;letbinaryFuncs: BinaryFunction[]=[];letunaryFunc: UnaryFunction=arg1=>{};letreduced=binaryFuncs.reduce((prev,next)=>prev,unaryFunc);// BEFORE: Error - Type 'BinaryFunction' is not assignable to type 'UnaryFunction'// AFTER: Works correctly - reduced is inferred as UnaryFunctionletf: UnaryFunction=reduced;

Solution

Swap the order of the two overloads that take an initialValue parameter:

  • Before: reduce(cb, initialValue: T) then reduce<U>(cb, initialValue: U)
  • After: reduce<U>(cb, initialValue: U) then reduce(cb, initialValue: T)

The same change is applied to both reduce and reduceRight in both ReadonlyArray<T> and Array<T>.

Fixes#7014

Reorder the overload declarations for reduce and reduceRight in both
ReadonlyArray<T> and Array<T> interfaces so the generic <U> overload
(with initialValue: U) is checked before the same-type overload
(with initialValue: T).
This fixes incorrect type inference when calling reduce/reduceRight
with an initial value of a different type than the array elements.
Fixesmicrosoft#7014
@github-project-automationgithub-project-automationBot moved this to Not started in PR BacklogJan 29, 2026
@typescript-bottypescript-bot added the For Backlog Bug PRs that fix a backlog bug label Jan 29, 2026
@taronsung

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

The overload order change affects the displayed type signature and
improves type inference in some cases (e.g., anyInferenceAnonymousFunctions
now correctly infers any[] instead of any when reduce is called with []
as the initial value).
@taronsung
taronsungforce-pushed the fix/reorder-array-reduce-overloads branch from c9dc0ef to 3efe7e1CompareJanuary 30, 2026 22:39
@taronsung
taronsungforce-pushed the fix/reorder-array-reduce-overloads branch from 3efe7e1 to 4601242CompareFebruary 2, 2026 05:23
@RyanCavanaugh

Copy link
Copy Markdown
Member

@typescript-bot test it

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey @RyanCavanaugh, this PR is in an unmergable state, so is missing a merge commit to run against; please resolve conflicts and try again.

ArshVermaGit

This comment was marked as spam.

@vrjordant

Copy link
Copy Markdown

Is there something that needs to be done before this can be merged?

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog BugPRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Consider re-ordering Array#reduce overloads in lib.d.ts

5 participants

@taronsung@RyanCavanaugh@typescript-bot@vrjordant@ArshVermaGit