reduce initialvalue partial type assertion
Add a signature to the type for Array.reduce<U> to allow the initialValue to be a Partial<U>
Many cases where the reduce function is used to build an object that will eventually conform to U, but starts with an empty accumulator.
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;
}
My suggestion meets these guidelines:
If anything, you should make your code,
declare function isU (partialU : Partial<U>) : partialU is U;
function doSomething (someArr : T[]) : U {
const partialResult : Partial<U> = someArr.reduce<Partial<U>>(
(memo, item) => {
memo[item.someField] = transform(item);
return memo;
},
{} /*Assuming your Partial<U> allows the empty object*/
);
if (isU(partialU)) {
return partialU;
} else {
throw new Error(`Failed to build U`);
}
}
If you're going to say "Oh, I know that it will build U for sure!"
function doSomething (someArr : T[]) : U {
const partialResult : Partial<U> = someArr.reduce<Partial<U>>(
(memo, item) => {
memo[item.someField] = transform(item);
return memo;
},
{} /*Assuming your Partial<U> allows the empty object*/
);
return partialU as U;
}
In my opinion, we do not need to make Array.reduce<>() less type-safe just for this.
This isn't really correct, so I'm going to say we're probably not going to take a change for this.
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:
interface Array<T> {
reduce<U, V = U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: V): U;
}
Or add it as an overload without the default for V. It could also be made more narrow by doing reduce<U extends V, V = U>.
Verbatim, what you pass for initialValue becomes the first parameter given to the callback - it doesn't make sense for it to have an independent type.
You say you want to avoid type assertions (saying, "Hey, TS! I know what I'm doing!"). But what you're suggesting is effectively doing the same thing.
You still tell TS, "I know that it looks like partial but it's actually not!"
Most helpful comment
If anything, you should make your code,
If you're going to say "Oh, I know that it will build
Ufor sure!"In my opinion, we do not need to make
Array.reduce<>()less type-safe just for this.