.map() .reduce() .filter() methods fail with "This expression is not callable." error on valid array with union type #36390
Comments
|
I think this is a duplicate of #7294 |
|
Duplicate of #7294 (see comment). Related to #29011. Mentioned in documentation as caveats for improved behavior for calling union types:
|
|
Is this really a duplicate? The 'duplicate' issue is marked as closed, but this issue still exists. If it really is a duplicate, can we re-open #7294 to handle this case? |
|
The reason stated by @weswigham for not unifying for generic signatures with multiple signatures is that:
But for function signatures originating from the same declaration we can be sure that we are talking about the same overload signatures in the same order and with the same generic parameters. As long as the generics don't have constraints, unifying the signatures should be safe IMO. I think that unifying for this case would satisfy a lot of use cases. |
|
Yeah, I have a change around here somewhere that allows it for exactly that situation rummages through bin of old PRs |
|
@weswigham What was wrong with it? Why did it get stuck ? Can we revive it :D ? |
|
#31023 contains the relevant code. It's a bit old, and some parts of it are probably no longer needed thanks to some other signature resolution changes we made (namely the base signature stuff should no longer be needed). |
|
Cool! So is this planned for a release in the future I'm guessing? I wanted to have a look at this over the weekend but glad to see it already there and we will get eventually :) |
|
Looks like my issue #36307 talks about pretty much the same behavior, linking for discoverability. Glad to see there's been some movement! |
|
Until this gets fix you can do something like: |
|
I got similar error while calling map on |
|
This issue is closed, but the const arr: number[] | string[] = [];
arr.reduce((acc: Array<string>, a: number | string, index: number) => {
return []
}, [])
/* This expression is not callable.
Each member of the union type '...' has signatures,
but none of those signatures are compatible with each other. */ |
|
I'm super excited about the improvements in #31023! But it doesn't seem to have completely resolved the issues with calling array methods on unions of arrays. I tested a few common array methods and found the following results:
I've created a new issue to track the remaining functionality. |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

TypeScript Version: any
Code:
Expected behavior: TS should let me iterate through array (I tell it the type is either array of numbers OR array of strings (not an array of mixed types). In any case the variable is an array and those methods should be callable.
Actual behavior: TS throws error "This expression is not callable."
The text was updated successfully, but these errors were encountered: