Definitelytyped: Express typings seem to choke on middleware

Created on 30 May 2018  ·  3Comments  ·  Source: DefinitelyTyped/DefinitelyTyped

  • [√] I tried using the @types/xxxx package and had problems.
  • [√] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [√ ] I have a question that is inappropriate for StackOverflow.
  • [ √] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors: @borisyankov @19majkel94 @kacepe @micksatana @samijaber

I'm having the current problem using @types/express 4.11.1 & TSC 2.8.3.

The following code cannot compile:

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

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

I get the following error:

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

Most helpful comment

Hi Jimmy,

You need to import like this...

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

The reason is Request, Response, NextFunction of Node.js are not the same as Express.

Hope this helps.

All 3 comments

Hi Jimmy,

You need to import like this...

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

The reason is Request, Response, NextFunction of Node.js are not the same as Express.

Hope this helps.

Thanks, I'll give that a try!

This works great. Thanks muchly, again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbreckmckye picture jbreckmckye  ·  3Comments

jgoz picture jgoz  ·  3Comments

csharpner picture csharpner  ·  3Comments

JudeAlquiza picture JudeAlquiza  ·  3Comments

fasatrix picture fasatrix  ·  3Comments