I am attempting to include extra properties in the session object
req.session.confirmationCode = confirmationCode;
However, I encounter an error stating that the property confirmationCode does not exist
Property 'confirmationCode' does not exist on type 'Session & Partial<SessionData>'.
I have an index.d.ts file located in the types directory where I define this property
declare global {
namespace session {
interface SessionData {
confirmationCode: number;
}
}
}
export {};
This is my tsconfig.json configuration file
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"removeComments": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"noImplicitAny": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noStrictGenericChecks": true
},
"exclude": ["node_modules"],
"include": ["src"]
}
In the source code for @types/express-session package, I found out that I can extend the session object like so
declare module "express-session" {
interface SessionData {
confirmationCode: number;
}
}
Yet, when I try this approach, I receive an error mentioning that the session function is not callable
Type 'typeof import("express-session")' has no call signatures
What is the correct way to properly extend the session object?
UPD1: Here is how I invoke the session function
app.use(
session({
name: "wishlify",
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 60, // 2 months
secure: process.env.NODE_ENV === "production",
},
})
);