Definitelytyped: Express-Eingaben scheinen an Middleware zu ersticken

Erstellt am 30. Mai 2018  ·  3Kommentare  ·  Quelle: DefinitelyTyped/DefinitelyTyped

  • [√] Ich habe versucht, das Paket @types/xxxx und hatte Probleme.
  • [√] Ich habe versucht, die neueste stabile Version von tsc zu verwenden. https://www.npmjs.com/package/typescript
  • [√] Ich habe eine Frage, die nicht für StackOverflow geeignet ist.
  • [ √] [Erwähnen](https://github.com/blog/821-mention-somebody-they-re-notified) die Autoren (siehe Definitions by: in index.d.ts ), damit sie es können Antworten.

    • Autoren: @borisyankov @19majkel94 @kacepe @miccksatana @samijaber

Ich habe das aktuelle Problem mit @types/express 4.11.1 & TSC 2.8.3.

Der folgende Code kann nicht kompiliert werden:

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

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

Ich bekomme folgenden Fehler:

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'.

Hilfreichster Kommentar

Hallo Jimmy,

Sie müssen so importieren ...

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);

Der Grund dafür ist, dass Request , Response , NextFunction von Node.js nicht mit Express identisch sind.

Hoffe das hilft.

Alle 3 Kommentare

Hallo Jimmy,

Sie müssen so importieren ...

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);

Der Grund dafür ist, dass Request , Response , NextFunction von Node.js nicht mit Express identisch sind.

Hoffe das hilft.

Danke, das werde ich mal ausprobieren!

Das funktioniert super. Vielen Dank noch einmal.

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen