Partial type for reduce initialValue
See original GitHub issueSearch Terms
reduce initialvalue partial type assertion
Suggestion
Add a signature to the type for Array.reduce<U>
to allow the initialValue to be a Partial<U>
Use Cases
Many cases where the reduce function is used to build an object that will eventually conform to U
, but starts with an empty accumulator.
Examples
Simple example where the initialValue does not satisfy U
but the final result will:
['one'].reduce<{ one: string }>((acc, i) => ({ [i]: i, ...acc }), {})
This yields
Argument of type ‘{}’ is not assignable to parameter of type ‘{ one: string; }’. Property ‘one’ is missing in type ‘{}’ but required in type ‘{ one: string; }’.
Calling the function without an initialValue causes problems because the initialValue needs to be an object for this to work.
The below type signature would fix this issue:
interface Array<T> {
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: Partial<U>): U;
}
Checklist
My suggestion meets these guidelines:
- [?] This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Partial type for reduce initialValue · Issue #28901 - GitHub
Many cases where the reduce function is used to build an object that will eventually conform to U , but starts with an...
Read more >When using Array.reduce() and the initial value is an empty ...
Given an interface with non-optional keys and an empty initial value, you need to type the reducer as Partial<T> , but by the...
Read more >TypeScript how to set types for accumulated Value and ...
I just want to know why I cannot set a type to my accumulator as a number. and how should I set types...
Read more >TypeScript: adjusting types in reduce function with an async ...
In TypeScript, I was using the Array.prototype.reduce() method to iterate on an array and populate an object based on an asynchronous ...
Read more >reduce(_:_:) | Apple Developer Documentation
A closure that combines an accumulating value and an element of the sequence into a new accumulating value, to be used in the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Narratively I still think that allowing for a Partial makes sense here as the whole point of a reduce operation is to accumulate a single value from a list. Also, the type assertions are exactly the thing I’m trying to avoid.
How about at least allowing the opportunity to type the initialValue separately, while defaulting to
U
:Or add it as an overload without the default for
V
. It could also be made more narrow by doingreduce<U extends V, V = U>
.If anything, you should make your code,
If you’re going to say “Oh, I know that it will build
U
for sure!”In my opinion, we do not need to make
Array.reduce<>()
less type-safe just for this.