@types/express 中是否有用于 Express 中间件功能的类型

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

现在我在自定义 .d.ts 文件中有这个:

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

然后我像这样引用该文件:

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

但是我想知道 Express 是否已经有中间件函数的类型?

是的。您还需要导入 RequestHandler。检查定义 here

import { RequestHandler } from 'express';

以下是我的处理方式:

import type { RequestHandler } from "express";

export const myMiddleware: RequestHandler = (req, res, next) => {
  // HANDLE REQUEST
  // RESPOND OR CALL NEXT()
};