Reputation: 721
This code causes an error, the text of which is given below:
function getHandler(handlers: number[] | string[]): any {
return handlers.map(handler => handler);
}
Error text:
error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '{ (this: [string, string, string, string, string], callbackfn: (value: string, index: number, ...' has no compatible call signatures.
Upvotes: 1
Views: 603
Reputation: 275927
Change number[] | string[]
to (number|string)[]
function getHandler(handlers: (number | string)[]): any {
return handlers.map(handler=>handler);
}
Smaller repro of the issue:
const handlers: string[] | number[] = [];
handlers.map(x => x);
Reason: Simply because the type system cannot figure out from string[]|number[]
that the item x
might be string|number
. The difficulty is that it is always either string
or number
and not really string|number
simultaneously.
If you want it supported you might want to raise it here : https://github.com/Microsoft/TypeScript/issues
Upvotes: 6