Repository navigation
Re-order Array#reduce and Array#reduceRight overloads in lib.es5.d.ts - #63980
sundeep8967 wants to merge 2 commits into
Conversation
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 | |||
| 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[] |
|
@microsoft-github-policy-service agree |
4e3faab to
fb9b660
Compare
dd2f499 to
768308c
Compare
| * @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; |
There was a problem hiding this comment.
This breaks values.reduce((acc, value) => acc.concat({}), []) for values: any[]: acc becomes never[]. The new baseline errors show the same regression.
|
Thanks for the review Jake Bailey (@jakebailey)! You're completely right — prioritizing the generic |
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).