24

Right now I have this in a custom .d.ts file:

import { Request, Response, NextFunction } from 'express';
export type MiddleWareFn = (req: Request, res: Response, next: NextFunction) => void;

and I reference that file like so:

router.use('/foo', <MiddleWareFn> function(req,res,next){});

however I am wondering if Express has typings for middleware functions already?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

2 Answers2

45

Yes. You need to import also RequestHandler. Check definition here

import { RequestHandler } from 'express';
Carloluis
  • 4,205
  • 1
  • 20
  • 25
9

Here is how I'm handling it:

import type { RequestHandler } from "express";

export const myMiddleware: RequestHandler = (req, res, next) => {
  // HANDLE REQUEST
  // RESPOND OR CALL NEXT()
};
cbdeveloper
  • 27,898
  • 37
  • 155
  • 336