Khusamov Sukhrob
Khusamov Sukhrob

Reputation: 721

Error with combining Array types? Error: Cannot invoke an expression whose type lacks a call signature

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

Answers (1)

basarat
basarat

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

Related Questions