为什么'类型'Request ' 中不存在占用的属性'用户'>

编程入门 行业动态 更新时间:2024-10-08 08:32:09

为什么'类型'Request
'
<a href=https://www.elefans.com/category/jswz/34/1771233.html style=中不存在占用的属性'用户'>"/>

为什么'类型'Request ' 中不存在占用的属性'用户'>

运行ts-node时出现以下错误。

我如下定义了d.ts以使用“ req.user”并应用了tsconfig.json。

Path: src/@types/express/index.d.ts

import { User } from '../../model/user/user.interface';

declare global {
    namespace Express {
        interface Request {
            user?: User['employeeId'];
        }
    }
}

tsconfig.json

{
    "compilerOptions": {
        "typeRoots": [
          "./node_modules/@types",
          "./src/@types"
        ],
        "rootDir": ".",
        "module": "CommonJS",
        "strict": true,
        "outDir": "dist",
        "baseUrl": "./src",
        "paths": {
            "*": ["node_modules/@types/*", "src/@types"]
        },
        "esModuleInterop": true
    },
    "include": ["src/**/*.ts"]
}

控制器

Path: src/api/posts/controller.ts

export const get = (req: Request, res: Response) => {
  ...
  const { user } = req;
  -> occrud Error
};

我想念什么?

运行ts-node时出现以下错误。我如下定义d.ts以使用“ req.user”并应用了tsconfig.json。路径:src/@types/express/index.d.ts从'../../ model / ...

回答如下:

问题是ts-node不能接您的类型扩展名,但是tsc可以。相关的GitHub Issue有更多详细信息,但TL; DR是您必须将客户类型放在node_modules/@types之前,即:

更多推荐

为什么'类型'Request ' 中不存在占用的属性'用户'>

本文发布于:2024-05-07 11:53:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   属性   类型   用户   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!