关于 Express Request,Typescript 声明合并失败

2023-12-05

当我提出明确请求时,我收到此错误。

error TS2339: Property 'test' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.

这是我扩展请求的测试代码。

app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/login' }),
  function(req: Request, res: Response) {
    req.test = 'aa'
    return console.log(req.test)
});

为了解决这个问题,我已经搜索了很多并且我做到了。

Make types/express/index.d.ts文件。我添加 typeRoots 于tsconfig.json像下面这样。

"typeRoots": ["./node_modules/@types", "./types"],

这是 types/express/index.d.ts 文件,我尝试了什么。

declare namespace Express {
    export interface Request {
        test: string
    }
}

// This is also fail
declare namespace Express {
    interface Request {
        test: string
    }
}

// This is also fail
declare global {
  namespace Express {
    interface Request {
        test: string
    }
  }
}

Even my vscode tell me req.test type is string, server show me error. enter image description here

这是控制台上的完整错误

[nodemon] starting `ts-node src/app.ts`

/app/node_modules/ts-node/src/index.ts:434
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
src/app.ts(67,9): error TS2339: Property 'test' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
src/app.ts(68,28): error TS2339: Property 'test' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.

    at createTSError (/app/node_modules/ts-node/src/index.ts:434:12)
    at reportTSError (/app/node_modules/ts-node/src/index.ts:438:19)
    at getOutput (/app/node_modules/ts-node/src/index.ts:578:36)
    at Object.compile (/app/node_modules/ts-node/src/index.ts:775:32)
    at Module.m._compile (/app/node_modules/ts-node/src/index.ts:858:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Object.require.extensions.<computed> [as .ts] (/app/node_modules/ts-node/src/index.ts:861:12)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)

这是我的 package.json,你可以看到我的 typescript 版本是 3.9.6

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "nodemon --exec ts-node src/app.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-session": "^1.17.1",
    "jsonwebtoken": "^8.5.1",
    "nodemon": "^2.0.4",
    "passport": "^0.4.1",
    "passport-google-oauth20": "^2.0.0",
    "ts-node": "^8.10.2"
  },
  "devDependencies": {
    "@types/body-parser": "^1.19.0",
    "@types/cors": "^2.8.6",
    "@types/express": "^4.17.7",
    "@types/express-session": "^1.17.0",
    "@types/jsonwebtoken": "^8.5.0",
    "@types/node": "^14.0.18",
    "@types/passport": "^1.0.4",
    "@types/passport-google-oauth20": "^2.0.3",
    "typescript": "^3.9.6"
  }
}

这是我所有的 tsconfig.json

{
  "compilerOptions": {
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "sourceMap": true,                     /* Generates corresponding '.map' file. */
    "outDir": "./dist",                        /* Redirect output structure to the directory. */
    "removeComments": true,                /* Do not emit comments to output. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "typeRoots": ["./node_modules/@types", "./types"],                       /* List of folders to include type definitions from. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  },
  "exclude": [
      "nod_modules",
      "**/*.spec.ts"
  ]
}

我花了时间并从昨天开始感到压力。我该如何解决它?


这似乎是您配置方式的问题ts-node。为了使事情简单并加快启动速度,我建议添加-T/--transpile-only给你的ts-node论据,这样你的start脚本变成nodemon --exec ts-node -T src/app.ts.

该选项可防止ts-node对代码运行类型检查。这样做有几个好处:

  1. 您可以避免之间的配置冲突tsc(TypeScript 编译器)和ts-node
  2. ts-node启动速度更快
  3. 即使编译器失败,您也可以运行服务器,这在开发中非常有用。

请注意,您never想要提交或合并无法正确编译的代码tsc。您可以通过在 CI(持续集成)上检查这一点或作为预提交挂钩来做到这一点。如果你添加"tsc": "tsc" to scripts in package.json你可以做npm run tsc or yarn tsc编译您的代码。

您可以阅读更多有关ts-nodeCLI 标志在这里:https://www.npmjs.com/package/ts-node#cli-and-programmatic-options

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关于 Express Request,Typescript 声明合并失败 的相关文章

随机推荐

  • R 将 x 或 y 坐标分配给栅格的像元以执行计算

    是否有任何解决方案可以使用 R 将 X 或 Y 坐标分配给光栅图像的所有单元格 例如 假设我有一个包含 3x3 像元的栅格 左下坐标为X 7 Y 15 以米为单位 X和Y方向分辨率均为 10 m X向右增加 Y向上增加 然后 我想生成栅格表
  • Android 表格视图

    我需要创建一个与此类似的表格布局http sourceforge net dbimage php id 194965我需要一些关于如何执行此操作的示例源代码 你确定它是一个TableLayout 对我来说它看起来像一个 ListView 如
  • 文本框中的超链接电子邮件地址并通过 Outlook 发送

    我正在开发一个 wpf 应用程序 我有一个客户信息部分 我可以在其中记录我的客户信息 在本节中 我使用一个文本框来记录客户的电子邮件地址 但现在我想制作电子邮件地址超链接 并通过 Outlook 电子邮件链接电子邮件地址 比如说 如果我单击
  • 如何使任何页面成为免费的 RSS 源?

    我想将其作为 RSS 源导入到我的网站上http www huffingtonpost com news yoga 但它不是 RSS 提要 赫芬顿邮报 确实有 RSS 提要 但范围太广了 我想要一本关于瑜伽的 我看到有些网站可以这样做 但你
  • 在 VESA 图形模式下绘制像素

    如何在VESA图形模式下绘制像素 我正在尝试中断10h功能0ch 但它不起作用 怎么了 注 我用 NASM 语法编写了这段代码 并使用 qemu 进行了测试 Code Mov ax 4F02h Mov bx 0105h 1024x768 p
  • 修复了 QGraphicsItem 的位置,而不改变场景中其他 QGraphicsItem 的行为

    这个问题与 强制 QGraphicsItem 保持原状 我想要一个QGraphicsItem在场景中移动时位于固定位置 建议的解决方案是覆盖void paintEvent QPaintEvent 子类的QGraphicsView void
  • 使用 xmlstarlet,如何更改元素的值

    使用 xmlstarlet 如何替换 ThreadGroup num threads 所有实例的值 Before
  • xcb_grab_key 上没有错误,但事件循环未捕获(全局热键)

    我正在尝试在 Linux 上设置全局热键 我最初使用的是 x11 libX11 so 但是我必须从线程中执行此操作 我尝试过但是XPendingEvent and XNextEvent最终会使应用程序崩溃 所以我切换到xcb libxcb
  • 帮助逆向工程二进制文件格式的工具

    有哪些工具可以帮助解码未知的二进制数据格式 我知道 Hex Workshop 和 010 Editor 都支持结构 对于已知的固定格式来说 这些在有限的范围内是可以的 但对于更复杂的东西来说很难使用 特别是对于未知的格式 我想我正在寻找脚本
  • 使用 Ruby 1.9.3 进行 ruby​​ 调试?

    我刚刚更新到 Ruby 1 9 3p0 和 Rails 3 1 1 现在 当我尝试启动服务器时 它抱怨我应该安装ruby debug 即使它已经安装了 rails server environment development debug g
  • CreateProcess 和 CreatePipe 在 VC++ 中执行进程并以字符串形式返回输出

    我正在尝试使用CreateProcess and CreatePipe从 Visual Studio 2010 中的 Windows Forms C CLR 应用程序中执行进程 在我的 Windows 表单应用程序中 我想执行一个子进程 控
  • EXPLAIN 和 COUNT 返回两个不同的值

    我在做 explain select from calibration 它说 52133456345632 行 当我做 select count from calibration 我收到 52134563456961 有人可以解释一下这里发
  • Python 中按下按键时播放声音

    我正在编写一个Python脚本 其中每次按下按键时都会播放声音 我正在使用 Winsound 模块来播放声音 我想要这样的东西 import winsound while True if any key is being pressed R
  • 使用 const char*、to_string() 和 c_str() 时出现奇怪的输出

    我有一个程序使用to string转换一个int to a string 然后使用将其转换为 C 字符串string c str 它存储在一个数组中const char 当程序输出每个const char 结果并不出乎我的意料 对于下面的示
  • 将对象数组中的所有 null 更改为 '' (javascript)

    我有如下所示的对象数组 Object Results Array 3 Results Array 3 0 2 0 Object id null name Rick Value 34343 1 Object id 2 name null Va
  • 在 x 个字符后更改 CSS

    我里面有一个文本字符串td 如下 td This is a long line td 我想要做的是更改前七个字符之后所有字符的字体大小 现在我知道我可以执行以下操作 td This is span class different a lon
  • 在 DataGridView 中显示 SQL 结果的问题

    我试图在我的应用程序的数据网格视图中显示 SQL 查询的结果 下面的代码是我写的 但它似乎与我做什么无关 我无法在表单的 datagridview 中得到任何显示 有人能指出我正确的方向吗 code Sql SELECT FROM jobL
  • awk 中的矩阵加法

    我有一堆变量 如下所示 DURATION 1 57 DURATION 2 07 T0 10 T1 0 TX 0 T0 12 T1 0 TX 1 TC 1 IG 0 TC 2 IG 3 是否可以让 awk 处理这个结果 结果是 DURATIO
  • .NET 对象大小

    net 中堆分配对象的大小是多少 包括管理开销 我假设对象是沿着 4 字节边界分配的 还是使用了不同的方法 x86 上的 4 字节边界 x64 上可能有 8 字节边界 x86 上有 8 个字节的开销 用于类型引用和同步块 如果发现 x64
  • 关于 Express Request,Typescript 声明合并失败

    当我提出明确请求时 我收到此错误 error TS2339 Property test does not exist on type Request