【问题标题】:Is there a type in @types/express for Express middleware functions@types/express 中是否有用于 Express 中间件功能的类型
【发布时间】:2018-08-07 19:46:11
【问题描述】:

现在我在自定义 .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 是否已经有中间件功能的类型?

【问题讨论】:

    标签: node.js express typescript2.0 tsc


    【解决方案1】:

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

    import { RequestHandler } from 'express';
    

    【讨论】:

    • 好的。随时将此标记为您问题的答案。干杯!
    【解决方案2】:

    这是我的处理方式:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 2013-11-08
      相关资源
      最近更新 更多