具体来说,我正在尝试为 express 设置服务器端打字稿编译。
公开的接口之一是 RequestHandler,具有以下结构:
// express-serve-static-core/index.d.ts
declare module "express-serve-static-core" {
...
interface RequestHandler {
(req: Request, res: Response, next: NextFunction): any;
}
}
我写了以下课程:
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'
有什么建议吗?我不确定我做错了什么。