In my experience with using TypeScript in ExpressJS, I have encountered several issues. One of them involves accessing data in the request and response parameters provided by third-party middleware like express-session.
Here is the code snippet that has been causing me trouble:
import express, {
Application,
Request,
Response
} from 'express';
const cli:Application = express();
cli.set('trust proxy', true);
cli.use(require('cookie-parser')());
cli.use(require('express-session')({secret: 'Help me!'}));
cli.get('/invites/:invite', (req:Request, res:Response) => {
if (req.params.invite){
req.session.code = req.params.invite;
console.log(req.session.code);
res.send(req.session.code);
}
});
Upon testing this code in my IDE, an error message pops up: https://i.sstatic.net/KC0AD.png
I have tried to resolve it by using the extended interface approach:
interface WithSession extends Request {
session: any
}
cli.get('/invites/:invite', (req:WithSession, res:Response) => {
if (req.params.invite){
req.session.code = req.params.invite;
console.log(req.session.code);
res.send(req.session.code);
}
});
However, my IDE continues to show errors: https://i.sstatic.net/b2MGC.png If you have any solutions to offer, please share them with me!
Thank you for taking the time to read about my issue. I am hopeful that we can find a resolution together!