Definitelytyped: يبدو أن الكتابة السريعة تختنق بالبرامج الوسيطة

تم إنشاؤها على ٣٠ مايو ٢٠١٨  ·  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 @ 19majkel94kacepemicksatanasamijaber

أواجه المشكلة الحالية باستخدام @ 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);

السبب هو Request ، Response ، NextFunction من Node.js ليست مثل 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);

السبب هو Request ، Response ، NextFunction من Node.js ليست مثل Express.

أتمنى أن يساعدك هذا.

شكرًا ، سأجرب ذلك!

هذا يعمل بشكل رائع. شكرا جزيلا مرة أخرى.

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات

القضايا ذات الصلة

alisabzevari picture alisabzevari  ·  3تعليقات

ArtemZag picture ArtemZag  ·  3تعليقات

demisx picture demisx  ·  3تعليقات

Loghorn picture Loghorn  ·  3تعليقات

fasatrix picture fasatrix  ·  3تعليقات