镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Re-order Array#reduce and Array#reduceRight overloads in lib.es5.d.ts - #63980

Closed
sundeep8967 wants to merge 2 commits into
microsoft:mainfrom
sundeep8967:fix/reorder-array-reduce-overloads
Closed

sundeep8967 wants to merge 2 commits into
microsoft:mainfrom
sundeep8967:fix/reorder-array-reduce-overloads

Conversation

@sundeep8967

Copy link
Copy Markdown

Problem

When calling Array#reduce or Array#reduceRight with an initial value of a different type or an explicit generic parameter, TypeScript previously evaluated the non-generic reduce(callbackfn: (previousValue: T, currentValue: T, ...) => T, initialValue: T): T overload before reduce<U>(callbackfn: (previousValue: U, currentValue: T, ...) => U, initialValue: U): U.

This forced users to add unnecessary type assertions or type annotations when reducing an array to an accumulator of a different type (such as reducing objects into a dictionary/record map).

Fixes #7014

Solution

  1. In tsc/internal/bundled/libs/lib.es5.d.ts and tsc/internal/bundled/source/es5.d.ts, place the generic reduce<U>(..., initialValue: U): U overload before the same-type reduce(..., initialValue: T): T overload on both Array<T> and ReadonlyArray<T>.
  2. Apply the same re-ordering to reduceRight<U> on Array<T> and ReadonlyArray<T>.
  3. Add compiler test case tsc/testdata/tests/cases/compiler/arrayReduceInference.ts and update baselines via hereby baseline-accept.

Testing

  • Ran compiler build: npx hereby tsc:build (completed cleanly).
  • Verified type checking with built compiler on array reduce expressions.
  • Ran test suite and updated baselines: npx hereby baseline-accept.
  • Ran unit tests: go test ./internal/checker/... ./internal/compiler/... ./internal/execute/tsc/... (100% passing).

Copilot AI balanced review requested due to automatic review settings August 24, 2026 14:40
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Aug 24, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Aug 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Reorders Array and ReadonlyArray reduction overloads to prioritize generic accumulator inference.

Changes:

  • Reorders reduce and reduceRight overloads in both ES5 library copies.
  • Adds an inference compiler test.
  • Updates affected compiler and fourslash baselines.

Reviewed changes

Copilot reviewed 29 out of 31 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tsc/internal/bundled/libs/lib.es5.d.ts Reorders bundled array reduction overloads.
tsc/internal/bundled/source/es5.d.ts Reorders source array reduction overloads.
tsc/testdata/tests/cases/compiler/arrayReduceInference.ts Adds reduction inference cases.
tsc/testdata/baselines/reference/fourslash/goToType/goToTypeDefinition_arrayType.baseline.jsonc Updates displayed overload order.
tsc/testdata/baselines/reference/conformance/restPropertyWithBindingPattern(target=es2015).types Updates inferred array method types.
tsc/testdata/baselines/reference/conformance/recursiveTypeReferences1.types Updates reduction signatures.
tsc/testdata/baselines/reference/conformance/parserharness.types Updates reduction signatures.
tsc/testdata/baselines/reference/compiler/unknownSymbolOffContextualType1.types Updates reduction signatures.
tsc/testdata/baselines/reference/compiler/unionOfClassCalls.types Records changed union-array inference.
tsc/testdata/baselines/reference/compiler/unionOfArraysFilterCall.types Updates union reduction signatures.
tsc/testdata/baselines/reference/compiler/thisInTupleTypeParameterConstraints.types Updates augmented overload order.
tsc/testdata/baselines/reference/compiler/spreadInvalidArgumentType.types Updates expanded array type.
tsc/testdata/baselines/reference/compiler/returnTypeParameterWithModules.types Updates prototype reduction signatures.
tsc/testdata/baselines/reference/compiler/restParameterWithBindingPattern3.types Updates expanded tuple type.
tsc/testdata/baselines/reference/compiler/restInvalidArgumentType.types Updates expanded array-rest type.
tsc/testdata/baselines/reference/compiler/restElementWithNumberPropertyName(target=es2015).types Updates expanded tuple-rest type.
tsc/testdata/baselines/reference/compiler/recursiveTypeRelations.types Records changed generic inference.
tsc/testdata/baselines/reference/compiler/mappedTypeWithAsClauseAndLateBoundProperty2.types Updates mapped array signatures.
tsc/testdata/baselines/reference/compiler/mappedTypeWithAsClauseAndLateBoundProperty2.js Updates emitted declaration order.
tsc/testdata/baselines/reference/compiler/mappedTypeWithAsClauseAndLateBoundProperty.types Updates mapped array signatures.
tsc/testdata/baselines/reference/compiler/mappedTypeWithAsClauseAndLateBoundProperty.errors.txt Updates diagnostic type rendering.
tsc/testdata/baselines/reference/compiler/inferFromGenericFunctionReturnTypes2.types Updates reduction signatures.
tsc/testdata/baselines/reference/compiler/inferFromGenericFunctionReturnTypes1.types Updates reduction signatures.
tsc/testdata/baselines/reference/compiler/genericReduce.types Updates generic reduction signatures.
tsc/testdata/baselines/reference/compiler/genericContextualTypingSpecialization.types Updates explicit-generic signatures.
tsc/testdata/baselines/reference/compiler/duplicateOverloadInTypeAugmentation1.types Updates augmented reduction signatures.
tsc/testdata/baselines/reference/compiler/destructuringTuple.types Records changed empty-array inference.
tsc/testdata/baselines/reference/compiler/destructuringTuple.errors.txt Updates resulting diagnostics.
tsc/testdata/baselines/reference/compiler/anyInferenceAnonymousFunctions.types Records never[] accumulator inference.
tsc/testdata/baselines/reference/compiler/anyInferenceAnonymousFunctions.symbols Updates resolved symbols.
tsc/testdata/baselines/reference/compiler/anyInferenceAnonymousFunctions.errors.txt Adds resulting overload errors.

@@ -0,0 +1,24 @@
// @target: es5
// @declaration: true
Comment on lines +9 to +12
const idMap = items.reduce((acc, item) => {
acc[item.id] = item;
return acc;
}, {} as Record<string, Item>);
arr.reduce((acc: Array<string>, a: number | string, index: number) => {
>arr.reduce((acc: Array<string>, a: number | string, index: number) => { return []}, []) : never[]
>arr.reduce : { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string; <U>(callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } | { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; <U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; }
>arr.reduce((acc: Array<string>, a: number | string, index: number) => { return []}, []) : string[]
@sundeep8967

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@sundeep8967
sundeep8967 force-pushed the fix/reorder-array-reduce-overloads branch from 4e3faab to fb9b660 Compare August 27, 2026 06:54
@sundeep8967
sundeep8967 force-pushed the fix/reorder-array-reduce-overloads branch from dd2f499 to 768308c Compare September 5, 2026 09:39
* @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks values.reduce((acc, value) => acc.concat({}), []) for values: any[]: acc becomes never[]. The new baseline errors show the same regression.

@sundeep8967

Copy link
Copy Markdown
Author

Thanks for the review Jake Bailey (@jakebailey)! You're completely right — prioritizing the generic reduce<U> overload causes empty array literals [] to infer U = never[] when values: any[], resulting in the baseline errors and breaking downstream accumulator typing. Given this regression and previous discussion in #7014 / #36554 regarding Array overload order changes, closing this PR. Thanks for taking the time to review!

@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Oct 13, 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: Done

Development

Successfully merging this pull request may close these issues.

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

3 participants