Extending session object
See original GitHub issueHi there, I know this is not an actual issue, but a help wanted.
I’m struggling with typescript and express session and I have been playing around and trying to fiugure out this one for quite a bit.
I’m trying to extend my session object, for that, I’m trying to do a merge of typings as per in the documentation:
I have a types/session.d.ts
with the following interface to be merged:
declare module 'express-session' {
interface SessionData {
userId: string;
}
}
But this is not working, as an example, in other-folder/some.ts
req.session.userId = user.id;
// Property 'userId' does not exist on type 'Session & Partial<SessionData>'.
However, if I import Session
from express-session
, it does work:
import { Session } from 'express-session'
declare module 'express-session' {
interface SessionData {
userId: string;
}
}
I’m not very proficient with TypeScript and I’m unsure about importing a module in a type defintion, event TypeScript complains about this (warning):
‘Session’ is declared but its value is never read.
I’m wondering, is this the right way of tackling the problem?
What can I do different?
Kind regards!
PS: my tsconfig should be fine as I have available through my code other type definitions and they are working with no issues at all.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
I can certainly reopen the issue, but I have no idea about anything Typescript related so cannot provide any answers myself on the topic. The typescript stuff is not even managed in this repository so there is nothing to merge into here to affect change around it, if that is needed.
Closing because of lack of input. Thanks @dougwilson 😃