简体   繁体   English

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

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

Specifically, I'm trying to setup server sided typescript compilation for express.具体来说,我正在尝试为 express 设置服务器端打字稿编译。

One of the interfaces exposed is RequestHandler, with the following structure:公开的接口之一是 RequestHandler,其结构如下:

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

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

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

I wrote the following 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);
    });
  }
}

However, this throws the error:但是,这会引发错误:

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'

Any suggestions out there?有什么建议吗? I'm not sure what I'm doing wrong.我不确定我做错了什么。

RequestHandler is an interface that is specifying something with a call signature, which classes cannot implement. RequestHandler 是一个接口,它指定了一些类无法实现的调用签名。 You want a regular function:你想要一个常规功能:

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

If the interface had new in front of the method signature, it would be defining the shape of the constructor of your class, but that isn't the case.如果接口在方法签名之前有new ,它将定义类的构造函数的形状,但事实并非如此。

Another way to think about it is this: when you use a class, you're defining a function that must be called with new .另一种思考方式是:当您使用一个类时,您正在定义一个必须使用new调用的函数。 Is Express going to call "new PageNotFound(...)" or is it calling "pageNotFound(...)"? Express 是要调用“new PageNotFound(...)”还是调用“pageNotFound(...)”?

As Ryan Cavanaugh, one of the TypeScript developers, put it 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更正式地说,实现接口的类是关于类的实例所具有的内容的契约...... – Ryan Cavanaugh 2012 年 11 月 15 日 23:57

You want to keep it simple, classes are for objects that are used more than once.您想保持简单,类用于多次使用的对象。 The module express provides for you the Router object with the correct properties. 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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM