Can't extend JWT interface? #4920
-
Question 💬So I've decided to use TypeScript for my latest project which includes NextAuth as well. Because of my use case I had to extend all the default types like so:
The problem here is that no matter what I do it seems that extending JWT doesn't work. When I try to access the types on the session object for example i see everything I added. But with the token objects I can only access the default ones. I used this as a workaround to get type safety:
but I still don't know why it doesn't work out of the box :/ How to reproduce ☕️
Contributing 🙌🏽Yes, I am willing to help answer this question in a PR |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
++ Same thing happens in other parts of code then as well, for example I can access all added session properties from session object I get out of getSession() but can't access added properties of token from getToken() |
Beta Was this translation helpful? Give feedback.
-
The declare module 'next-auth/jwt' {
/** Returned by the `jwt` callback and `getToken`, when using JWT sessions */
interface JWT {
user?: {
name: string;
sex: string;
};
}
} |
Beta Was this translation helpful? Give feedback.
-
While this is the accepted answer for v4, I figured I'd ask how to do this for v5 of next-auth? Here's everything I've tried so far: declare module '@auth/core/types' {
interface JWT {
test: string;
}
} declare module '@auth/core/jwt' {
interface JWT {
test: string;
}
} declare module 'next-auth/jwt' {
interface JWT extends DefaultJWT {
user: {
test: string
};
}
} I'm able to extend the User interface, just not the JWT |
Beta Was this translation helpful? Give feedback.
-
I'm also very confused about it because they say in the documentation that JWT is deprecated but it still needs it somehow and this type extention is a huge problem here, because when I remove everything about JWT, I'm getting type errors in the console, plus typing for Session is also problematic because I extend the interface with my user data but then when I call it in a component, it says that the type is missing. This whole next auth works great without TS but with it it's a huge pain. |
Beta Was this translation helpful? Give feedback.
The
JWT
type is not exported fromnext-auth
, butnext-auth/jwt
. Could you try it like this?