1

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!

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.