Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Express typings seem to choke on middleware #26146

Closed
jbreckmckye opened this issue May 30, 2018 · 5 comments
Closed

Express typings seem to choke on middleware #26146

jbreckmckye opened this issue May 30, 2018 · 5 comments

Comments

@jbreckmckye
Copy link

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'.
@micksatana
Copy link
Contributor

micksatana commented May 31, 2018

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.

@jbreckmckye
Copy link
Author

Thanks, I'll give that a try!

@jbreckmckye
Copy link
Author

jbreckmckye commented May 31, 2018

This works great. Thanks muchly, again.

@jonah-cod
Copy link

Thanks this works

@dhawel
Copy link

dhawel commented Nov 27, 2022

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants