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 intomicrosoft: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 intomicrosoft:mainfrom
creazyfrog:fix/reduce-overload-order

Conversation

@creazyfrog
Copy link
Copy Markdown

@creazyfrog creazyfrog commented Apr 26, 2026

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 before reduce<U>(callbackfn, initialValue: U): U. This caused TypeScript to infer the less-specific T[] type instead of a caller-supplied U type:

const result = [1, 2, 3].reduce((acc, x) => { acc.push(x); return acc; }, []);
//    ^^^^^^ 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

const nums = [1, 2, 3];
const result = 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

Fixes microsoft#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-automation github-project-automation Bot moved this to Not started in PR Backlog Apr 26, 2026
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Apr 26, 2026
@creazyfrog creazyfrog 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 first Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs 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