Given is the function signature below:
JavaScript
x
2
1
function foo(): string[] | number[]
2
Why does TS complain about the follow function call of filter?
JavaScript
1
4
1
foo().filter((v) => true);
2
^^^^^^
3
Error
4
This expression is not callable. Each member of the union type ‘{ (predicate: (value: number, index: number, array: number[]) => value is S, thisArg?: any): S[]; (predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): number[]; } | { …; }’ has signatures, but none of those signatures are compatible with each other.
Of course I can cast it to []
, but what is the proper way here? The error message is very difficult to understand? How would one decipher this?
Example: Playground
Advertisement
Answer
JavaScript
1
2
1
function foo(): (number|string)[]
2
For same reason as here: Typescript: How to map over union array type?