@types/express-jwt
& @types/passport
package together and had problems.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
: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.
Most helpful comment
How to type request.user now: