gpt4 book ai didi

node.js - typescript 错误 TS2393 : Duplicate function implementation in a Node/Express app

转载 作者:太空宇宙 更新时间:2023-11-04 03:08:05 25 4
gpt4 key购买 nike

我有一个使用 TypeScript 的 Node + Express 应用程序。我有一些 API 路由,我在每个路由中声明了一个函数,如下所示:

function cleanReqBody(req) {
req.body.createdBy = req.user;
req.body.modifiedBy = req.user;
req.body.modified = new Date();
}

当针对我的代码运行 tsc 时,我得到TS2393:重复函数实现。我真的不明白为什么。我可以理解如果这是客户端代码,它会认为两个函数都在同一范围(窗口)上,但为什么这适用于 Node 模块?我的每个 Node 路由不是都使用不共享范围的单独 Node 模块吗?

最佳答案

Aren't each of my node routes using separate node modules that don't share scope

That is true at runtime. However TypeSript will only know of this fact if your file contains a root level import or export statement.(more)

如果您修改每个文件并出现如下错误:

function cleanReqBody(req) {
req.body.createdBy = req.user;
req.body.modifiedBy = req.user;
req.body.modified = new Date();
}
export let something = 123;

问题应该会消失。

PS:请注意,如果您不使用 import/export,您就会错过 TypeScript 理解 Node.JS 模块系统并帮助您维护文件模块的一大优势。

关于node.js - typescript 错误 TS2393 : Duplicate function implementation in a Node/Express app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33355011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com