对于Express中间件函数,@ types / express中是否有类型

时间:2018-02-28 02:10:52

标签: node.js express typescript2.0 tsc

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

.replaceAll("[\\([0-9]\\%)]","");

我像这样引用该文件:

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

但是我想知道Express是否已经为中间件功能打字?

2 个答案:

答案 0 :(得分:4)

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

import { RequestHandler } from 'express';

答案 1 :(得分:2)

这是我的处理方式:

import type { RequestHandler } from "express";

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