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

Created on 28 Feb 2018  路  17Comments  路  Source: DefinitelyTyped/DefinitelyTyped

  • [x] I tried using the @types/express-jwt & @types/passport package together and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • Authors: @horiuchi, @enaeseth, @theigor, @tlaziuk, @danielpa9708, @wokim, @kacepe, @Sl1MBoy

Hello!
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.

Greetings,
Lietzi

Most helpful comment

How to type request.user now:

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

declare namespace Express {
  export interface User extends UserModel {}
}

All 17 comments

:thinking: Changing to user?: User in @types/express-jwt would not be appropriate for express-jwt does not depend on passport

Maybe creating a pacakge just for holding the user definition and make passport and express-jwt depend on it ?

@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 c75ee379eb78e44d3c7a04d0ab4f10ed93625c1d

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

I've got the same problem with passport + express-jwt. Pinned passport also to previous version 0.4.2 to fix it.

Skip libs until it's fixed:

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

Any update on this?

don't think so, have any ideas ?

Also experiencing issues. Unsure the best way to resolve.

@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;
        }
    }
}

It's ok by me, that would solve the problem,
I'm not sure if this is the right approach though

Hm... In my opinion it is the right approach because developers who uses @types/express-jwt without passport should be able to extend their req.user object also. And looks like there is no other way than define extendable interface.

Ok, seems fair

How to type request.user now:

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

declare namespace Express {
  export interface User extends UserModel {}
}

Fully redefining User within Express namespace also works

declare global {
    namespace Express {
        interface User {
            customerId: number
        }
    }
}

How to type request.user now:

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

declare namespace Express {
  export interface User extends UserModel {}
}

Where is the appropriate place to put this? Thank you!

@wcjord put this anywhere that gets loaded in your codebase - I prefer to have it as close as possible to the entry point. Also, I had to use @omakoleg's solution with declare global.

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
               ~~~~

  node_modules/@types/passport/index.d.ts:24:13
    24             user?: User;
                   ~~~~
    'user' was also declared here.


Found 1 error.
Was this page helpful?
0 / 5 - 0 ratings