Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript union type array `a[] | b[]` is not `reduce`able, but `map`able

I'm trying to reduce over a union of array-types: foo[] | bar[]. With map it works as expected and I get an element of foo|bar in the lamda, but it does not work with reduce. I get the error, that 'the expression is not callable, because the signatures are not compatible'.

Maybe this is related, but that was in TS 3.6 days, and I'm using 4.5. Is there a (neat) way around this?

I have a Typescript Playground here

const m = [] as Array<number | string>;
m.reduce((it) => it); // ok
m.map((it) => it); // ok

const m2 = [] as Array<number> | Array<string>;
m2.reduce((it) => it);
m2.reduce((it) => it as any);  // not even with 'any'-lambda
// This expression is not callable.
// Each member of the union type '<OMITTED> ...' has signatures, but none of those signatures are compatible with each other.
m2.map((it) => it); // why does `map` work then ?

// same with tuples
const m3 = [1, 1] as [number, number] | [string];
m3.map((it) => it);
m3.reduce((it) => it); // same error
like image 648
Joel Avatar asked Oct 14 '25 04:10

Joel


1 Answers

In addition to jcalz comment, this is a known restriction of TS, but there's a workaround that you shift the type predicate job to generic inference instead of a whole type predicate of which array type is

function map<T extends number | string>(arr: T[]) {
    return arr.map(v => v); // no error
}

function reduce<T extends number | string>(arr: T[]) {
    return arr.reduce(it => it); // no error
    // return arr.reduce((acc, cur) => acc + (typeof cur === "number" ? cur : cur.length), 0); // no error either, with some practical logic
}
like image 106
crazyones110 Avatar answered Oct 18 '25 16:10

crazyones110