0

While using typescript in ExpressJS... I've run into many problems, one of then is me trying to access data in the request and response parameters provided by third party middleware such as express-session.

I have this code here:

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);
  
    }
});

when I put this code in my IDE I get this error: error

I have already attempted the fix using the extended interface:


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);
  
    }
});

but my IDE still gives errors: err2 If there is any way to fix this please let me know!

Thanks for reading my problem, I hope you can solve it!

Artrix
  • 19
  • 4

1 Answers1

0

The answer is to install @types/express-session, and possibly to turn off strict null checks. See here for more discussion.

Josh Wulf
  • 4,171
  • 2
  • 16
  • 29