Cannot invoke an expression whose type lacks a call signature ... has no compatible call signatures

27,728

The problem is that the two functions have different signatures so the result of the indexing operation will be a union of the two signatures which by definition will not be callable.

You can use the Function methods call or apply which are accessible (since they are common to both signatures in the union) to call the function, with the disadvantage of losing all type safety:

if (typeof firstDeferedCmd == "object" && ( firstDeferedCmd.type === "execute" || firstDeferedCmd.type === "execSpawn" )) {
    ProcessPoolExecutor[firstDeferedCmd.type].call(ProcessPoolExecutor, firstDeferedCmd);
}

You can always just use an assertion to make the union callable, but this is not any safer then call:

if (typeof firstDeferedCmd == "object" && ( firstDeferedCmd.type === "execute" || firstDeferedCmd.type === "execSpawn" )) {
    (ProcessPoolExecutor[firstDeferedCmd.type] as (cmd: IDeferedCmd) => Promise<{stdout: string;stderr: string;}> | Promise<NodeJS.ReadableStream>)(firstDeferedCmd);
}

You could also use two checks to sperate out the two different signatures, and this actually exposes an issue with your current design:

function fn(firstDeferedCmd : IDeferedCmd){
    if (typeof firstDeferedCmd == "object") {
        if(firstDeferedCmd.type === "execute") {
            return ProcessPoolExecutor[firstDeferedCmd.type](firstDeferedCmd);
        }
        if(firstDeferedCmd.type === "execSpawn") {
            if(firstDeferedCmd.args){
                return ProcessPoolExecutor[firstDeferedCmd.type](firstDeferedCmd);  // error since there is no requirement if execSpawn is specified to also specify args
            }
        }
    }
}

We can fix this by changing the definition of the IDeferedCmd:

type IDeferedCmd = {
    type: "execute",
    cmd: string,
    name: string,
} | {
    type: "execSpawn",
    cmd: string,
    name: string,
    resolve: IResolveFn,
    reject: IRejectFn,
    args: Array<string>,
    options: object


}
function fn(firstDeferedCmd : IDeferedCmd){
    if (typeof firstDeferedCmd == "object") {
        if(firstDeferedCmd.type === "execute") {
            return ProcessPoolExecutor[firstDeferedCmd.type](firstDeferedCmd);
        }
        if(firstDeferedCmd.type === "execSpawn") {
            if(firstDeferedCmd.args){
                return ProcessPoolExecutor[firstDeferedCmd.type](firstDeferedCmd);  // ok now
            }
        }
    }
}
Share:
27,728
Amit Wagner
Author by

Amit Wagner

build build build

Updated on July 09, 2022

Comments

  • Amit Wagner
    Amit Wagner almost 2 years

    i get an error

    Cannot invoke an expression whose type lacks a call signature ... has no compatible call signatures.
    

    on one of my methods and i cant figure out how to fix it. i have seen this link cannot-invoke-an-expression-whose-type-lacks-a-call-signature

    and 2 more but still didn`t mange to figure it out

    type declaration :

    type ProcessMethods = "execute" | "execSpawn"
    
    interface IDeferedCmd {
        type: ProcessMethods,
        cmd: string,
        name: string,
        resolve: IResolveFn,
        reject: IRejectFn,
        args?: Array<string>,
        options?: object
    
    
    }

    in my class i have 2 static methods that looks like this

    static execute({cmd, name}: { cmd: string, name: string }): Promise<{
            stdout: string;
            stderr: string;
        }>
    
    static execSpawn({cmd, name, args , options }: { cmd: string, name: string, args: Array<string>, options: object }): Promise<NodeJS.ReadableStream>

    and a third method in witch the error is thrown from try to call them dynamicly

    if (typeof firstDeferedCmd == "object" && ( firstDeferedCmd.type === "execute" || firstDeferedCmd.type === "execSpawn" )) {
                    ProcessPoolExecutor[firstDeferedCmd.type](firstDeferedCmd); // this line throw the error
    }

    and the error it self

    Cannot invoke an expression whose type lacks a call signature. Type '(({ cmd, name }: { cmd: string; name: string; }) => Promise<{}>) | (({ cmd, name, args, options }...' has no compatible call signatures. ProcessPoolExecutorfirstDeferedCmd.type; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~