我无法在Express的请求对象中声明新属性

发布于 2025-02-04 02:37:13 字数 1816 浏览 2 评论 0原文

我正在尝试将新属性附加到打字稿中的请求对象。 这是代码:

import { request, Request, response, Response } from "express";

((req: Request, res: Response) => {
    console.log(req.user);
})(request, response)

我是这样声明的:

declare global {
   namespace Express {
      interface Request {
         user: string;
      }
   }
}

然后我用TS节点运行它。结果是:

/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:843
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
x.ts:9:21 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

9     console.log(req.user);
                      ~~~~

    at createTSError (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:843:12)
    at reportTSError (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:847:19)
    at getOutput (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:1057:36)
    at Object.compile (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:1411:41)
    at Module.m._compile (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:1596:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.require.extensions.<computed> [as .ts] (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:1600:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
  diagnosticCodes: [ 2339 ]
}

我测试了太多网站的答案,但是其中一个没有起作用。请帮忙。

I'm trying to attach the new property to the request object in typescript.
this is the code :

import { request, Request, response, Response } from "express";

((req: Request, res: Response) => {
    console.log(req.user);
})(request, response)

i'm declaring like this :

declare global {
   namespace Express {
      interface Request {
         user: string;
      }
   }
}

and then I'm running it with ts-node. result is :

/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:843
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
x.ts:9:21 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.

9     console.log(req.user);
                      ~~~~

    at createTSError (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:843:12)
    at reportTSError (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:847:19)
    at getOutput (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:1057:36)
    at Object.compile (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:1411:41)
    at Module.m._compile (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:1596:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.require.extensions.<computed> [as .ts] (/home/mahdi/Desktop/learn-stuf/test/node_modules/ts-node/src/index.ts:1600:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
  diagnosticCodes: [ 2339 ]
}

I tested too many answers of sites, but one of them did not work. please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒人说梦 2025-02-11 02:37:13
  1. 首先,我认为您的声明文件遇到了一些问题。
    编辑文件之类的文件
export {}

declare global {
   namespace Express {
      interface Request {
         user: string;
      }
   }
}

,或

namespace Express {
  interface Request {
    user?: string
  }
}

  1. 添加包含tsconfig中的声明文件的目录。由于我通常将其命名express.d.ts,然后放在src/types文件夹中,在我的情况下,tsconfig.json将被编辑最后
{
  "compilerOptions": {
    "typeRoots": ["src/types"],
  }
}

  1. ,还将在tsconfig.json中添加TS节点配置。 (不在compileroptions中)
{
  "ts-node": {
    "files": true
  }
}
  1. First, I think your declare file got some problems.
    edit the file like
export {}

declare global {
   namespace Express {
      interface Request {
         user: string;
      }
   }
}

or

namespace Express {
  interface Request {
    user?: string
  }
}

  1. add directory that contains the declare file in tsconfig. Since I usually name it express.d.ts and place in src/types folder, in my case, tsconfig.json will be edited like this
{
  "compilerOptions": {
    "typeRoots": ["src/types"],
  }
}

  1. lastly, also add ts-node configuration in tsconfig.json. (not in compilerOptions)
{
  "ts-node": {
    "files": true
  }
}
芯好空 2025-02-11 02:37:13

您是否正在寻找@type/express/express/express/express

您还可以使用

function endpoint (req: Request, res: Response & {user: string;}) {
  console.log(req.user);
}

但是,也许您正在寻找req.body.user,type 响应&lt; {user:string;}&gt;

Are you maybe looking for @types/express ?

You can also fix it with intersection type :

function endpoint (req: Request, res: Response & {user: string;}) {
  console.log(req.user);
}

But maybe you are looking for req.body.user, type Response<{user: string;}> ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文