Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
taronsung wants to merge 4 commits intomicrosoft: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 intomicrosoft:mainfrom
taronsung:fix/reorder-array-reduce-overloads

Conversation

@taronsung
Copy link

Summary

Reorder the overload declarations forreduce andreduceRight in bothReadonlyArray<T> andArray<T> interfaces so the generic<U> overload (withinitialValue: U) is checked before the same-type overload (withinitialValue: T).

Problem

When callingreduce 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 theinitialValue: 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 aninitialValue parameter:

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

The same change is applied to bothreduce andreduceRight in bothReadonlyArray<T> andArray<T>.

Fixes#7014

Reorder the overload declarations for reduce and reduceRight in bothReadonlyArray<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/reduceRightwith an initial value of a different type than the array elements.Fixesmicrosoft#7014
@taronsung
Copy link
Author

@microsoft-github-policy-service agree

The overload order change affects the displayed type signature andimproves type inference in some cases (e.g., anyInferenceAnonymousFunctionsnow correctly infers any[] instead of any when reduce is called with []as the initial value).
@taronsungtaronsungforce-pushed thefix/reorder-array-reduce-overloads branch fromc9dc0ef to3efe7e1CompareJanuary 30, 2026 22:39
@taronsungtaronsungforce-pushed thefix/reorder-array-reduce-overloads branch from3efe7e1 to4601242CompareFebruary 2, 2026 05:23
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

For Backlog BugPRs that fix a backlog bug

Projects

Status: Not started

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

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

2 participants

@taronsung@typescript-bot

Comments


[8]ページ先頭

©2009-2026 Movatter.jp