Is there a type in @types/express for Express middleware functions

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?

2 Answers

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

import { RequestHandler } from 'express'; 
1

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() }; 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like