没有重载与此调用匹配。类型'字符串'不能分配给类型'信号'

2022-03-07 00:00:00 signals node.js javascript typescript

我正在使用TypeScript构建微服务并处理信号。直到几天前,代码还运行良好,但最近开始抛出错误。找不到该问题的修复程序。

处理信号的代码。这只是文件的一部分。 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)

任何修复都将不胜感激。或者您是否能说出为什么它在两天前才开始工作,而不是现在。


解决方案

当您向匿名函数传递了错误数量的参数时,有时会出现这种情况:

    Object.keys(data).reduce((key: string) => {

    }, {}); 

将引发错误:

没有重载与此调用匹配。重载%1,共%3

向其传递正确数量的参数:

    Object.keys(data).reduce((acc: any, key: string) => {
    }, {});

相关文章