node.jstypescriptexpress

Argument of type '(req: Request, res: IResponse, next: NextFunction) => void' is not assignable to parameter of type 'PathParams' with express.js


I'm creating a route handler and I want to add it in my routes:

import { Request, Response, NextFunction } from "express";

interface IResponse extends Response {
  error: (code: number, message: string) => Response;
  success: (code: number, message: string, result: any) => Response
}
const routeHandler = (req: Request, res: IResponse, next: NextFunction) => {
  res.error = (statusCode: number, errorMessage: string) => res.status(statusCode).json(errorMessage);
  res.success = (statusCode: number, message: string, result: any) => res.status(statusCode).json({
    message,
    result
  });
  return next();
};

export default routeHandler;

when I added this handler to the routes index file:

import { Router } from "express";
import routeHandler from "../utils/helpers";

const routes = Router();
routes.use(routeHandler);

export default routes;

I got this error:

No overload matches this call.
  The last overload gave the following error.
    Argument of type '(req: Request, res: IResponse, next: NextFunction) => void' is not assignable to parameter of type 'PathParams'.
      Type '(req: Request, res: IResponse, next: NextFunction) => void' is missing the following properties from type '(string | RegExp)[]': pop, push, concat, join, and 25 more.ts(2769)
index.d.ts(55, 5): The last overload is declared here.

Solution

  • Express does not know anything about your interface IResponse. So the method cannot match.

    To achieve your idea, use module augmentation

    import { Request, Response, NextFunction } from "express";
    
    declare module 'express-serve-static-core' {
        interface Response {
            error: (code: number, message: string) => Response;
            success: (code: number, message: string, result: any) => Response
        }
    }
    
    const routeHandler = (req: Request, res: Response, next: NextFunction) => {
        res.error = (statusCode: number, errorMessage: string) => res.status(statusCode).json(errorMessage);
        res.success = (statusCode: number, message: string, result: any) => res.status(statusCode).json({
            message,
            result
        });
        return next();
    };
    
    export default routeHandler;