Definitelytyped: 快速打字似乎在中间件上窒息

创建于 2018-05-30  ·  3评论  ·  资料来源: DefinitelyTyped/DefinitelyTyped

  • [√] 我尝试使用@types/xxxx包,但遇到了问题。
  • [√] 我尝试使用最新的稳定版 tsc。 https://www.npmjs.com/package/typescript
  • [√] 我有一个不适合StackOverflow 的问题
  • [ √] [提及](https://github.com/blog/821-mention-somebody-they-re-notified) 作者(见Definitions by:中的index.d.ts )所以他们可以回应。

    • 作者: @borisyankov @19majkel94 @kacepe @micksatana @samijaber

我在使用 @types/express 4.11.1 & TSC 2.8.3 时遇到了当前问题。

以下代码无法编译:

function aMiddleware(req: Request, res: Response, next: NextFunction) {
    next();
}

app.get('/foo/bar', aMiddleware);

我收到以下错误:

TS2345: Argument of type '(req: Request, res: Response, next: NextFunction) => void' is not assignable to parameter of type 'RequestHandlerParams'.
  Type '(req: Request, res: Response, next: NextFunction) => void' is not assignable to type '(RequestHandler | ErrorRequestHandler)[]'.
    Property 'includes' is missing in type '(req: Request, res: Response, next: NextFunction) => void'.

最有用的评论

嗨,吉米,

你需要像这样导入...

import { Express, Request, Response, NextFunction } from 'express';
const express = require('express');

const app: Express = express();

function aMiddleware(req: Request, res: Response, next: NextFunction) {
    next();
}

app.get('/foo/bar', aMiddleware);

原因是 Node.js 的Request , Response , NextFunction和 Express 不一样。

希望这可以帮助。

所有3条评论

嗨,吉米,

你需要像这样导入...

import { Express, Request, Response, NextFunction } from 'express';
const express = require('express');

const app: Express = express();

function aMiddleware(req: Request, res: Response, next: NextFunction) {
    next();
}

app.get('/foo/bar', aMiddleware);

原因是 Node.js 的Request , Response , NextFunction和 Express 不一样。

希望这可以帮助。

谢谢,我试试看!

这很好用。 非常感谢,再次。

此页面是否有帮助?
0 / 5 - 0 等级