Skip to content

Conversation

@majiayu000
Copy link

Summary

This PR reorders the Array#reduce and Array#reduceRight method overloads to fix incorrect type inference when the initial value type differs from the array element type.

Problem

When using reduce with an initialValue whose type U is assignable to the array element type T (e.g., number assignable to {}), TypeScript incorrectly matches the reduce(callbackfn, initialValue: T): T overload instead of the more specific generic reduce<U>(callbackfn, initialValue: U): U overload.

Example (from issue):

let arrayOfObjs: {}[] = [];
let countOfProps = arrayOfObjs.reduce((sum, obj) => sum + Object.keys(obj).length, 0);
// Error: Operator '+' cannot be applied to types '{}' and 'number'

Solution

Move the generic overload reduce<U> before the reduce(initialValue: T) overload:

// Before
reduce(callbackfn: ...) => T): T;
reduce(callbackfn: ..., initialValue: T): T;
reduce<U>(callbackfn: ..., initialValue: U): U;

// After
reduce(callbackfn: ...) => T): T;
reduce<U>(callbackfn: ..., initialValue: U): U;  // moved up
reduce(callbackfn: ..., initialValue: T): T;

Files Changed

  • src/lib/es5.d.ts - ReadonlyArray and Array interfaces
  • src/lib/esnext.iterator.d.ts - Iterator interface
  • Updated test baselines to reflect new overload order

Test plan

  • All existing reduce-related tests pass
  • Test baselines updated to reflect new overload ordering
  • Verified fix with reproduction cases from issue

Fixes #7014

🤖 Generated with Claude Code

@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Dec 11, 2025
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Dec 11, 2025
@majiayu000 majiayu000 force-pushed the fix/7014-reorder-reduce-overloads branch 2 times, most recently from b8b0e55 to ca2a562 Compare December 11, 2025 05:41
)

The generic overload `reduce<U>(callbackfn, initialValue: U): U` is now
placed before `reduce(callbackfn, initialValue: T): T` to ensure better
type inference when the initial value type differs from the array element type.

This fixes cases where `U` is assignable to `T` (e.g., `number` assignable
to `{}`), which previously caused the non-generic overload to match incorrectly.

Fixes microsoft#7014

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@majiayu000 majiayu000 force-pushed the fix/7014-reorder-reduce-overloads branch from ca2a562 to 84a6bbf Compare December 11, 2025 05:48
@majiayu000
Copy link
Author

Closing this PR as the implementation needs further review. The JSDoc comments were not properly moved with their corresponding function overloads. Will resubmit after proper analysis.

@majiayu000 majiayu000 closed this Dec 11, 2025
@github-project-automation github-project-automation bot moved this from Not started to Done in PR Backlog Dec 11, 2025
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: Done

Development

Successfully merging this pull request may close these issues.

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

2 participants