admin管理员组文章数量:1290945
i get an error
Cannot invoke an expression whose type lacks a call signature ... has no patible 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
}
i get an error
Cannot invoke an expression whose type lacks a call signature ... has no patible 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
Share Improve this question edited Aug 20, 2018 at 7:20 Jonathan Hall 79.8k19 gold badges159 silver badges203 bronze badges asked Aug 20, 2018 at 7:19 Amit WagnerAmit Wagner 3,2643 gold badges22 silver badges39 bronze badgesCannot invoke an expression whose type lacks a call signature. Type '(({ cmd, name }: { cmd: string; name: string; }) => Promise<{}>) | (({ cmd, name, args, options }...' has no patible call signatures. ProcessPoolExecutorfirstDeferedCmd.type; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 Answer
Reset to default 4The 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 mon 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
}
}
}
}
本文标签:
版权声明:本文标题:javascript - Cannot invoke an expression whose type lacks a call signature ... has no compatible call signatures - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741507303a2382395.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论