请帮助,我收到此错误
src/app/middlewares/authentication.ts:16:17 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
16 req.user = user;
我已经创建了 .d.ts 文件并将其包含在 tsconfig 文件中。我仍然无法运行这段代码
请查找随附的屏幕截图
- 创建一个typessrc 目录中的文件夹
- 使用您要扩展的包的名称在 types 文件夹中创建一个文件夹。 (在这种情况下express).
- 创建一个索引.d.ts该文件夹中的文件
src/
- types/
- express/
- index.d.ts
- 将此代码添加到索引文件中
import express from "express";
declare global {
namespace Express {
interface Request {
user?: Record<string,any>
}
}
}
- 记得更新你的tsconfig.json file
{
"compilerOptions": {
"typeRoots" : ["./src/types", "./node_modules/@types"]
}
}
这应该有效
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)