![](/img/trans.png)
[英]No overload matches this call. The last overload gave the following error. Type '“text”' is not assignable to type '“json”'
[英]No overload matches this call. Type 'string' is not assignable to type 'Signals'
我正在使用 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)
任何修复将不胜感激。 或者,如果您能说出为什么它在 2 天前而不是现在更早地工作。
当您将错误数量的参数传递给匿名函数时,有时会发生这种情况:
Object.keys(data).reduce((key: string) => {
}, {});
将引发错误:
没有重载匹配此调用。 重载 1 of 3
传递正确数量的参数:
Object.keys(data).reduce((acc: any, key: string) => {
}, {});
我也遇到了这个奇怪的问题,但我使用类型断言(在我的情况下使用字符串枚举)解决了它:
(Object.values(someEnum) as string[]).concat(otherStringArray);
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
这样的数字枚举包括一个反向映射。 例如,您可以执行以下操作:
const r1 = signals.SIGHUP // r1 value: 1
const r2 = signals[signals.SIGINT] // r2 value: "SIGINT"
const r3 = signals[15] // r3 value: "SIGTERM"
这就是为什么你得到Object.values(signals)
的(string | signals)[]
,其中string
代表枚举键, signals
代表枚举值。
现在, process.on(signal, ...)
中的参数signal
必须是预定义的 Node.JS 字符串文字类型之一。 但是我们传入string | signals
string | signals
项目类型,所以 TS 在这一点上大喊大叫。
当您没有正确分配类型时,就会发生这种情况。
您的变量数据类型必须与传递的数据类型匹配。
organizationList: { id: string; name: string; }[] = [] //if you do this way it type error
organizationList: Organization[] = [] //correct defining method
get Org() {
return this.list.filter((org: Organization) => org.id == this.id) //this data type must defined data type
}
关于这种错误发生在我身上的一个奇怪案例可能是例如
somePath = somePath.replace(/\//g, /\\\//g);
改为使用
somePath= somePath.replace("/", "\/");
somePath= somePath.replace(/\//, "\/");
因为replace
中的第二个参数是字符串类型。
当我将非异步 function 传递给需要异步 function 的方法时,我遇到了这种 typescript 错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.