So looking at the express-session type declaration, it declares a Session (and modified Request object) under the Express namespace. Using this we can create a type declaration (mySession.dt.s) to augment the default properties, bypassing the limits of declaration merging:
import {Express} from 'express';
import 'express-session';
declare module 'express' {
export interface Request {
session: Express.Session & {
myOwnData: string;
myOwnData2: number;
myOwnData3: boolean;
};
}
}
Note that it seems that the compiler is somewhat flexible about the imports in this file (like it doesn't seem to care if Express or Request is imported), but being explicit will be most consistent.
We can then import this declaration into our server file:
import express = require('express');
import {Express, Request, Response} from 'express';
import './mySession';
import * as session from 'express-session';
const app: Express = express();
const PORT = process.env.PORT || process.env.NODE_PORT || 3000;
app.use('/endpoint', (req: Request, res: Response) => {
const a: number = req.session.myOwnData3 + 2; // fails to compile and highlighted by editors
console.log(req.session.myOwnData); // compiles and provided autocomplete by Webstorm
// do stuff
return res.ok();
});
app.use(session(/** do session stuff **/));
log.info(`Start Express Server on ${PORT}`);
app.listen(PORT);
As tested, this structure achieves type-safety on the added properties, and intellisense/autocomplete in both VSCode and WebStorm with both the express-session and added properties.
Unfortunately, as you noted, this won't be applied globally where type inference is utilized, (only where you explicitly import Request). If you want full control over the interface, you could uninstall @types/express-session and just copy & modify the d.ts (and import that). Another possibility is to declare a totally new property and implement, but obviously a lot more work.
express- Aravindinterface MyRequest extends Request { session: string }- degesession(I am on mobile). Second, you could try to type alias before and use the aliased type as the static type forsession. - Stefan Hanke