我正在使用打字稿来构建微服务并处理信号。直到几天前,代码都可以正常工作,但是最近它开始引发错误。找不到解决该问题的方法。
用于处理信号的代码。它只是文件的一部分。
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])
})
})
当我做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)
任何修复将不胜感激。或者,如果您能说出为什么它在2天前而不是现在就可以工作的原因。
答案 0 :(得分:5)
当您向匿名函数传递了不正确数量的参数时,有时会发生这种情况:
while line:
if 'Capture' in line:
parts = line.split()
name = parts[1]
number = parts[2]
line = input('Command: ')
elif 'Query' in line and parts[1] in line:
print(name, 'is level', number + '.')
line = input('Command: ')
elif 'Capture' in line and parts[1] == name:
part3 = line.split()
notname2 = part3[1]
print('You are already training', notname2 + '!')
line = input('Command: ')
elif 'Query' in line and parts[1] not in line:
part2 = line.split()
notname = part2[1]
print('You have not captured', notname, 'yet.')
line = input('Command: ')
else:
print('Unknown command!')
line = input('Command: ')
会引发错误:
没有重载匹配此调用。重载1/3
传递正确数量的参数:
Object.keys(data).reduce((key: string) => {
}, {});
答案 1 :(得分:2)
我也遇到了这个奇怪的问题,但是我使用类型断言(在我的情况下使用字符串枚举)解决了这个问题:
(Object.values(someEnum) as string[]).concat(otherStringArray);
答案 2 :(得分: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
})
// 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
})
enum signals2 {
SIGHUP = "SIGHUP",
SIGINT = "SIGINT",
SIGTERM = "SIGTERM"
}
Object.values(signals2)
.forEach(signal => {
process.on(signal, ...) // works now
})
诸如signals
之类的数字枚举包含一个reverse mapping。例如,您可以执行以下操作:
const r1 = signals.SIGHUP // r1 value: 1
const r2 = signals[signals.SIGINT] // r2 value: "SIGINT"
const r3 = signals[15] // r3 value: "SIGTERM"
这就是为什么您将(string | signals)[]
返回Object.values(signals)
的原因,其中string
代表枚举键,signals
代表枚举值。
现在,process.on(signal, ...)
中的参数signal
必须是predefined Node.JS string literal types之一。但是,我们传入了string | signals
项目类型,因此TS在这一点上大喊。