Skip to content

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

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

Re-order Array#reduce and Array#reduceRight overloads in lib.es5.d.ts#63980
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

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