Skip to content

fix(lib): reorder Array#reduce/reduceRight overloads so generic <U> variant is tried first - #63440

Open
creazyfrog wants to merge 1 commit into
microsoft:mainfrom
creazyfrog:fix/reduce-overload-order
Open

fix(lib): reorder Array#reduce/reduceRight overloads so generic <U> variant is tried first#63440
creazyfrog wants to merge 1 commit into
microsoft:mainfrom
creazyfrog:fix/reduce-overload-order

Conversation

@creazyfrog

@creazyfrogcreazyfrog commented Apr 26, 2026

Copy link
Copy Markdown

What this change intends to do

Fixes#7014

TypeScript resolves overloads by picking the first matching one. In src/lib/es5.d.ts, the reduce(callbackfn, initialValue: T): T overload was listed beforereduce<U>(callbackfn, initialValue: U): U. This caused TypeScript to infer the less-specific T[] type instead of a caller-supplied U type:

constresult=[1,2,3].reduce((acc,x)=>{acc.push(x);returnacc;},[]);// ^^^^^^ was inferred as: number[][] should be: number[]

Fix

Move the generic <U> overload above the T-with-initialValue overload in all four affected groups so TypeScript tries the more-specific overload first:

  • ReadonlyArray.reduce
  • ReadonlyArray.reduceRight
  • Array.reduce
  • Array.reduceRight

No new overloads are added and no signatures are changed — only the declaration order.

File changed:src/lib/es5.d.ts — 4 insertions, 4 deletions (declaration order swap only)

Verification

constnums=[1,2,3];constresult=nums.reduce<string[]>((acc,x)=>[...acc,String(x)],[]);// result: string[] ✓

Tests

This PR changes only declaration order in src/lib/es5.d.ts. No new compiler test cases are included. Existing baseline tests that exercise Array.reduce and Array.reduceRight continue to pass. A test covering the initialValue: U inference improvement (e.g. reduce((acc, x) => ..., []) inferring T[] rather than T[][]) would be the right addition — I can add one if a maintainer confirms this approach before I invest the time.

AI Assistance Disclosure

This PR was developed with the assistance of Claude Code (Anthropic), as required to be disclosed per the CONTRIBUTING.md guidelines.

…ariant is tried first
Fixesmicrosoft#7014. When TypeScript resolves overloads it picks the first matching one.
The `reduce(callbackfn, initialValue: T): T` overload was listed before
`reduce<U>(callbackfn, initialValue: U): U`, so `arr.reduce((acc, x) => ..., [])` inferred
`T[]` instead of the expected `U`. Moving the generic `<U>` overload above the `T`-with-initialValue
overload makes TypeScript try the more-specific version first. Applied to all four groups:
ReadonlyArray.reduce, ReadonlyArray.reduceRight, Array.reduce, Array.reduceRight.
Signed-off-by: creazyfrog <rohitcse.gec@gmail.com>
@github-project-automationgithub-project-automationBot moved this to Not started in PR BacklogApr 26, 2026
@typescript-bottypescript-bot added the For Backlog Bug PRs that fix a backlog bug label Apr 26, 2026
@creazyfrogcreazyfrog changed the title fix(lib): reorder Array#reduce/reduceRight overloads so generic <U> variant is tried first (#7014)fix(lib): reorder Array#reduce/reduceRight overloads so generic <U> variant is tried firstApr 27, 2026
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

2 participants

@creazyfrog@typescript-bot