1
votes

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.

1

1 Answers

6
votes

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