在 TypeScript 中,如何使用 (a: type, b:type): any 实现接口?

In TypeScript, how do I implement an interface with (a: type, b:type): any?

具体来说,我正在尝试为 express 设置服务器端的打字稿编译。

暴露的接口之一是RequestHandler,结构如下:

// express-serve-static-core/index.d.ts

declare module "express-serve-static-core" {
  ...

  interface RequestHandler {
    (req: Request, res: Response, next: NextFunction): any;
  }
}

我写了以下class:

import * as express from "express";

class PageNotFound implements express.RequestHandler {

  constructor (req: express.Request, res: express.Response, next: express.NextFunction) {
    let viewFilePath: string = "404";
    let statusCode: number = 404;
    let result: Object = {
      status: statusCode,
    };

    res.status(statusCode);
    res.render(viewFilePath, {}, function (err: Error, html: string): void {
      if (err) {
        res.status(statusCode).json(result);
      }
      res.send(html);
    });
  }
}

但是,这会引发错误:

error TS2345: Argument of type 'typeof PageNotFound' is not assignable to parameter of type 'RequestHandler'. Type 'typeof PageNotFound' provides no match for the signature '(req: Request, res: Response, next: NextFunction): any'

有什么建议吗?我不确定我做错了什么。

RequestHandler 是一个接口,它指定带有调用签名的内容,classes 无法实现。您想要一个常规函数:

function pageNotFound(req: express.Request, res: express.Response, next: express.NextFunction) {
    ...
}

如果接口在方法签名前面有 new,那么它将定义 class 的构造函数的形状,但事实并非如此。

另一种思考方式是:当您使用 class 时,您定义了一个必须使用 new 调用的函数。 Express 是调用 "new PageNotFound(...)" 还是调用 "pageNotFound(...)"?

正如 TypeScript 开发人员之一 Ryan Cavanaugh 所说 here:

More formally, a class implementing an interface is a contract on what an instance of the class has... – Ryan Cavanaugh Nov 15 '12 at 23:57

您想保持简单,类 用于多次使用的对象。模块 express 为您提供了具有正确属性的 Router 对象。

import * as express from 'express';
const router = express.Router();

router.get('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
    res.render('index', {
        title: 'Express'
    })
});

export = router;