Typescript: Partial type for reduce initialValue

Created on 7 Dec 2018  路  5Comments  路  Source: microsoft/TypeScript

Search 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
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
Awaiting More Feedback Suggestion

Most helpful comment

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.

All 5 comments

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!"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uber5001 picture uber5001  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

Zlatkovsky picture Zlatkovsky  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments