Attempting to enhance the Request
interface of Express
like so:
import express, { Request, Response } from 'express';
interface IRequest extends Request {
user: {
id: string;
}
}
const router = express.Router();
router.get('/', auth, async (req: IRequest, res: Response) => {
try {
const user = await User.findById(req.user.id).select('-password');
res.json(user);
} catch (e) {
console.error((e as Error).message);
res.status(500).send('Server Error');
}
});
Encountered this error during the process:
No overload matches this call. Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs>[]): Router', gave the following error. Argument of type '(req: IRequest, res: Response) => Promise' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'. Types of parameters 'req' and 'req' are incompatible. Property 'user' is missing in type 'Request<ParamsDictionary, any, any, ParsedQs>' but required in type 'IRequest'. Overload 2 of 3, '(path: PathParams, ...handlers: RequestHandlerParams<ParamsDictionary, any, any, ParsedQs>[]): Router', gave the following error. Argument of type '(req: IRequest, res: Response) => Promise' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs>'. Type '(req: IRequest, res: Response) => Promise' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs>'.ts(2769)