'How do I refactor this code so it doesn't throw this error "Property 'patient' does not exist on type 'Request<ParamsDictionary>"?

Here's is my code. I'm trying to use passport authenticate to store the info of patients specific to the token created for each person.

router.get("/current", passport.authenticate("jwt", { session: false }), (req: Request, res: Response) => {
    res.json({
        id: req.patient.id,
        fullName: req.patient.fullName,
        age: req.patient.age,
        email: req.patient.email,
        phoneNumber: req.patient.phoneNumber,
        address: req.patient.address,
        medicalHistory: req.patient.medicalHistory,

    });
});



Solution 1:[1]

You can use a type intersection like this:

req: Request & { patient: Record<string, any>}

Solution 2:[2]

I solved this problem by creating an interface that extends the Request type.

interface CustomRequest extends Request {
   patient?: any;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 David Momoh