node.js - 如何访问声明全局的 namespace ?

标签 node.js typescript express multer

背景

我正在制作一个简单的网站,用户可以在其中更新图片。 因此,我将 Multer 库和 Express 与 Typescript 结合使用。

问题

我想使用File type of Multer 。但我不知道如何访问 Multer 命名空间。

declare global {
  namespace Express {
    interface Request {
      file: Multer.File;
      files: {
        [fieldname: string]: Multer.File[];
      } | Multer.File[];
    }

  namespace Multer {
    interface File {
      /** Field name specified in the form */
      fieldname: string;
      ...
}

我的最终目标是这个。

app.post('/admin/uploads', async (req, res) => {
    let file: Multer.File = req.files.myFile;
    console.log(file.filename);
    ....
}

如何访问Multer命名空间?

最佳答案

app.post('/admin/uploads', async (req, res) => {
    let file: Express.Multer.File = req.files.myFile;
    console.log(file.filename);
    ....
}

关于node.js - 如何访问声明全局的 namespace ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56492987/

相关文章:

typescript - 如何在 Vuex 模块中使用 RxJS

json - 在javascript中导入json似乎没有成功

node.js - 为 MEAN 应用程序生成 Mongoose DTO

node.js - Node JS Express 的路由 GET 和 POST 是一样的

node.js - 错误 'env' 未被识别为内部或外部命令?

node.js - 如何在docker容器中连接graphql和prisma?

javascript - 在sequelize中查询两个表

javascript - 使用 Node.js 子进程时没有回车?

typescript - [嵌套接口(interface)/类型] : How to declare function return types inside a class in Typescript?

node.js - React 客户端获取的 URL 与服务器端请求的 URL 不匹配