Search code examples
typescriptexpressasync-awaitmiddleware

How do you type an async express middleware function in Typescript?


How do you define a return value for an express middleware that uses async/await? I've tried a few different things but haven't had much luck.

I'm also trying to overload the Request object to allow req.user to be defined when we decode the JWT payload.

import { NextFunction, Request, RequestHandler, Response } from 'express'

const authenticate: RequestHandler = async (req: IUserRequest, res: Response, next: NextFunction): Promise<void> => {
  if (! req.headers.authorization) { return next(new ValidationError()) }
  const payload: IUserData = await decodeJwt(req.headers.authorization)
  req.user = payload
  return next()
}

export interface IUserRequest extends Request {
  user: IUserData
}

/* errors 
(req: IUserRequest, res: Response, next: NextFunction) => Promise<void>' is not assignable to type 'RequestHandler'.
  Types of parameters 'req' and 'req' are incompatible.
    Type 'Request' is not assignable to type 'IUserRequest'.
      Property 'user' is missing in type 'Request'.
*/

Solution

  • Request and IUserRequest types are incompatible, because user is missing in Request. The property likely doesn't present in req until it's assigned to it.

    It should be:

    export interface IUserRequest extends Request {
      user?: IUserData
    }