Given the following code:
type Foo = {
foo: number
}
type Bar = {
bar: string
}
type FooOrBar =
| Foo
| Bar
type FooOrBarList =
| Foo[]
| Bar[]
function processFooOrBar(x: Foo): Foo;
function processFooOrBar(x: Bar): Bar;
function processFooOrBar(x: FooOrBar): FooOrBar {
return x
}
function processFooOrBarList(x: Foo[]): Foo[];
function processFooOrBarList(x: Bar[]): Bar[];
function processFooOrBarList(x: FooOrBarList): FooOrBarList {
return x.map(processFooOrBar)
}
I get the following error on x.map(processFooOrBar)
This expression is not callable. Each member of the union type ' (<U>(callbackfn: (value: Foo, index: number, array: Foo[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: Bar, index: number, array: Bar[]) => U, thisArg?: any) => U[])' has signatures, but none of those signatures are compatible with each other.ts(2349)
I do understand what the error message tells me but I dont get why.
Is this a compiler bug (because the compiler should be able to unify those) or am I running against some hard limits concerning type theory?