Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
合并Express请求对象和Passport.js用户/会话的Typescript声明_Typescript_Express_Types_Passport.js_Typescript Typings - Fatal编程技术网

合并Express请求对象和Passport.js用户/会话的Typescript声明

合并Express请求对象和Passport.js用户/会话的Typescript声明,typescript,express,types,passport.js,typescript-typings,Typescript,Express,Types,Passport.js,Typescript Typings,我在express/nodejs应用程序中使用passportjs中间件进行身份验证。尽管遵循了的步骤,但我的request.user对象的属性仍出现错误 我在项目根目录中的/types/index.d.ts处创建了一个文件,并将以下内容添加到了我的tsconfig.json中 "typeRoots": [ "/types/index.ts", "node_modules/@types" ] 我的全局声明合并如下所示: impor

我在express/nodejs应用程序中使用passportjs中间件进行身份验证。尽管遵循了的步骤,但我的request.user对象的属性仍出现错误

我在项目根目录中的/types/index.d.ts处创建了一个文件,并将以下内容添加到了我的tsconfig.json中

"typeRoots": [
  "/types/index.ts", "node_modules/@types"
]
我的全局声明合并如下所示:

import { User } from "../users/user.interface";
declare global {
  namespace Express {
    export interface Request {
        user: User;
    }
  }
}
**更新** 通过将声明合并更改为模块扩充,我已使
req.user
对象停止抛出错误,如下所示:

import { User } from "../users/user.interface";

declare module "express-serve-static-core" {
    interface Request {
        user: User;
    }
}
引用
request.session
对象时,我遇到以下错误:
类型“request”上不存在属性“session”。

passport的@types/passport types不应该与express的请求对象合并以添加会话吗


有人知道我如何成功地消除这些错误/让TS识别此合并声明吗。我是typescript新手,能够解决我遇到的大多数问题,但这一问题一直在出现。

要扩展Passport使用的
用户类型,您可以将您的声明合并为:


由提供的类型定义不定义由定义的
global.Express.Request#session

谢谢@samthecodingman,我重新安装了@types/Express session包并更改了导入,这似乎已被清除。我已经像你在这里发布的那样声明了合并,但这不起作用。不过,将其声明为“express serve static core”的模块扩展确实有效。
declare global {
  namespace Express {
    interface User {
        /* declare your properties here */
    }
  }
}