Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
104
rated 0 times [  107] [ 3]  / answers: 1 / hits: 31527  / 6 Years ago, mon, august 20, 2018, 12:00:00

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;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



More From » typescript

 Answers
15

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
}
}
}
}

[#53696] Wednesday, August 15, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
masonm

Total Points: 167
Total Questions: 87
Total Answers: 103

Location: Rwanda
Member since Wed, Jun 8, 2022
2 Years ago
masonm questions
;