SolvedDefinitelyTyped global declaration of Express.Request.user collides between @types/express-jwt and @types/passport

I used @types/express-jwt together with @types/passport in a project and getting this compile error:
node_modules/@types/express-jwt/index.d.ts(52,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'user' must be of type 'User', but here has type 'any'.

@types/express-jwt declaration of user?: any is colliding with the definition in @types/passport where the global property user of the Request interface in the Express namespace is defined as Type User.

I changed user?: any locally in @types/express-jwt to user?: User but does not know if this is appropriate for a PR.


19 Answers

✔️Accepted Answer

How to type request.user now:

type UserModel = import("../src/server/models/User").default;

declare namespace Express {
  export interface User extends UserModel {}

Other Answers:

@danielpa9708 thank you for your quick response..

I digged a little deeper in this issue..
at a first glimpse i thought User was declared in Express but as you mentioned
it is declared in passport and so it is not a good idea to create a dependency here..
furthermore i found, that it is just introduced by your commit c75ee37

So i think for now i will go back to the previous version
and look for a proper solution later on..

Skip libs until it's fixed:

"compilerOptions": { "skipLibCheck": true },

@danielpa9708 what do you think about to add the same User interface from @types/passport to @types/express-jwt?

// @types/express-jwt
declare global {
    namespace Express {
        export interface Request {
            user?: User

        interface User {
            [_: string]: any;

I am using NestJS, and declaring a global User did not work for me. Still getting this error:

node_modules/@types/express-jwt/index.d.ts:43:13 - error TS2717: Subsequent property declarations must have the same type.  Property 'user' must be of type 'User', but here has type 'any'.

43             user?: any

    24             user?: User;
    'user' was also declared here.

Found 1 error.

More Issues: