Javascript 没有重载匹配这个调用。“字符串”类型不可分配给“信号”类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/57722093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No overload matches this call. Type 'string' is not assignable to type 'Signals'
提问by M A SIDDIQUI
I am using typescript to build a micro service and handling signals as well. The code was working fine till few days ago but recently it started throwing error. Couldn't find a fix for the issue.
我正在使用打字稿来构建微服务并处理信号。直到几天前代码运行良好,但最近它开始抛出错误。找不到解决此问题的方法。
code for handling signals. It is just part of the file.
src/main.ts
处理信号的代码。它只是文件的一部分。
src/main.ts
enum signals {
SIGHUP = 1,
SIGINT = 2,
SIGTERM = 15
}
const shutdown = (signal, value) => {
logger.warn("shutdown!")
Db.closeAll()
process.exit(value)
}
Object.values(signals).forEach(signal => {
process.on(signal, () => {
logger.warn(`process received a ${signal} signal`)
shutdown(signal, signals[signal])
})
})
When I do ts-node src/main.tsThe following error throws and fails and exit.
当我做ts-node src/main.ts以下错误抛出并失败并退出时。
/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:245
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ? Unable to compile TypeScript:
src/main.ts:35:16 - error TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type 'string | signals' is not assignable to parameter of type 'Signals'.
Type 'string' is not assignable to type 'Signals'.
35 process.on(signal, () => {
~~~~~~
node_modules/@types/node/base.d.ts:653:9
653 on(event: Signals, listener: SignalsListener): this;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The last overload is declared here.
at createTSError (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:245:12)
at reportTSError (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:249:19)
at getOutput (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:362:34)
at Object.compile (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:395:32)
at Module.m._compile (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:473:43)
at Module._extensions..js (module.js:663:10)
at Object.require.extensions.(anonymous function) [as .ts] (/home/meraj/.nvm/versions/node/v8.10.0/lib/node_modules/ts-node/src/index.ts:476:12)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
Any fix would be appreciated. Or If you can tell why it was working earlier just 2 days ago and not now.
任何修复将不胜感激。或者,如果您能说出为什么它在 2 天前更早起作用而不是现在起作用。
回答by kitsu.eb
I also had this strange issue, but I worked around it using type assertions (in my case using a string enum):
我也有这个奇怪的问题,但我使用类型断言(在我的例子中使用字符串枚举)解决了它:
(Object.values(someEnum) as string[]).concat(otherStringArray);
回答by Donato
This sometimes happens when you have passed an incorrect number of arguments to an anonymous function:
当您将不正确数量的参数传递给匿名函数时,有时会发生这种情况:
Object.keys(data).reduce((key: string) => {
}, {});
will raise error:
会引发错误:
No overload matches this call. Overload 1 of 3
没有重载匹配这个调用。重载 1 of 3
Pass it the correct number of arguments:
将正确数量的参数传递给它:
Object.keys(data).reduce((acc: any, key: string) => {
}, {});
回答by ford04
Solution 1: Keep numeric enumsignals
解决方案 1:保留数字枚举signals
Object.values(signals)
// numeric enum includes reverse mapping, filter numbers out and keep "SIGHUP" etc.
.filter((s): s is NodeJS.Signals => typeof s !== "number")
.forEach(signal => {
process.on(signal, ...) // works now
})
Solution 2: Use pure signal string literal types
解决方案 2:使用纯信号字符串文字类型
// these string literal items are strongly typed by built-in NodeJS.Signals type
Object.values<NodeJS.Signals>(["SIGHUP", "SIGINT", "SIGTERM"])
.forEach(signal => {
process.on(signal, ...) // works now
})
Solution 3: Change to string enum(no reverse mapping)
方案三:改成字符串枚举(无反向映射)
enum signals2 {
SIGHUP = "SIGHUP",
SIGINT = "SIGINT",
SIGTERM = "SIGTERM"
}
Object.values(signals2)
.forEach(signal => {
process.on(signal, ...) // works now
})
Why does the error happen?
为什么会发生错误?
Numeric enums like signalsinclude a reverse mapping. For example you can do the following:
数字枚举signals包括反向映射。例如,您可以执行以下操作:
const r1 = signals.SIGHUP // r1 value: 1
const r2 = signals[signals.SIGINT] // r2 value: "SIGINT"
const r3 = signals[15] // r3 value: "SIGTERM"
That is why you get (string | signals)[]back for Object.values(signals), where stringstands for the enum keys and signalsfor the enum values.
这就是为什么你(string | signals)[]回来 for Object.values(signals), wherestring代表枚举键和signals枚举值。
Now, parameter signalin process.on(signal, ...)must be one of the predefined Node.JS string literal types. However we pass in string | signalsitem type, so TS yells at this point.
现在,参数signalinprocess.on(signal, ...)必须是预定义的 Node.JS 字符串文字类型之一。但是我们传入了string | signals项目类型,所以 TS 在这一点上大喊大叫。

