I'm using Passport to handle authentication with an Express application. This sets the user on the Express response. I'm using TypeScript, so setting the request type to Request in the route definitions will error because the user object does not exist on the Express Request. There are numerous questions about extending the request, either by declaration merging or extending the interface but these cause another error. My file looks like this:
import { Router, Request, Response } from 'express'
import { User as CustomUser } from './user'
interface IRequest extends Request {
user: CustomUser
}
const router: Router = Router()
router.get('/', requiresLogin, (req: IRequest, res: Response) => {
console.log(req.user)
res.sendStatus(204)
})
But now I'm getting the following TypeScript on the express callback:
Argument of type '(req: IRequest, res: Response) => void' is not assignable to parameter of type 'RequestHandlerParams'. Type '(req: IRequest, res: Response) => void' is not assignable to type '(RequestHandler | ErrorRequestHandler)[]'. Property 'includes' is missing in type '(req: IRequest, res: Response) => void'.
I've recently upgraded to TypeScript v2.8.3, and never had the problem previously.