Re-order Array#reduce and Array#reduceRight overloads in lib.es5.d.ts - #63980
Open
sundeep8967 wants to merge 2 commits into
Open
Re-order Array#reduce and Array#reduceRight overloads in lib.es5.d.ts#63980sundeep8967 wants to merge 2 commits into
sundeep8967 wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Reorders Array and ReadonlyArray reduction overloads to prioritize generic accumulator inference.
Changes:
- Reorders
reduceandreduceRightoverloads 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[] |
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When calling
Array#reduceorArray#reduceRightwith an initial value of a different type or an explicit generic parameter, TypeScript previously evaluated the non-genericreduce(callbackfn: (previousValue: T, currentValue: T, ...) => T, initialValue: T): Toverload beforereduce<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
tsc/internal/bundled/libs/lib.es5.d.tsandtsc/internal/bundled/source/es5.d.ts, place the genericreduce<U>(..., initialValue: U): Uoverload before the same-typereduce(..., initialValue: T): Toverload on bothArray<T>andReadonlyArray<T>.reduceRight<U>onArray<T>andReadonlyArray<T>.tsc/testdata/tests/cases/compiler/arrayReduceInference.tsand update baselines viahereby baseline-accept.Testing
npx hereby tsc:build(completed cleanly).npx hereby baseline-accept.go test ./internal/checker/... ./internal/compiler/... ./internal/execute/tsc/...(100% passing).