Seeking assistance with TypeScript and Express session integration.
I've been exploring ways to extend my session object, specifically through merging typings based on the documentation provided:
In my types/session.d.ts
file, I have the following interface that needs to be merged:
declare module 'express-session' {
interface SessionData {
userId: string;
}
}
However, I am encountering an issue where the merge is not working as expected. For instance, in other-folder/some.ts
:
req.session.userId = user.id;
// Property 'userId' does not exist on type 'Session & Partial<SessionData>'.
On the other hand, importing Session
from express-session
seems to resolve the error:
import { Session } from 'express-session'
declare module 'express-session' {
interface SessionData {
userId: string;
}
}
Despite this solution, I am hesitant about importing a module in a type definition, especially since TypeScript issues a warning stating:'Session' is declared but its value is never read.
With limited expertise in TypeScript, I'm unsure if this approach is correct. Any suggestions on what could be done differently?
Your insights are much appreciated!
PS: While my tsconfig setup appears to be correct and functional for other type definitions, this specific scenario presents a challenge.