最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin24浏览0评论

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

为什么'类型'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之前,即:

发布评论

评论列表(0)

  1. 暂无评论