Express Typings Broken in TS 2.2.x (VS Code, WebStorm, pretty much everything)

import express = require('express');
import { NextFunction } from "@types/express-serve-static-core";

const router:express.Router = express.Router();

router.get('/', (req: Request, res: Response, next: NextFunction) => {

});

export = router;

Possibly Related: microsoft/TypeScript#11875

Error Message:

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 ‘push’ is missing in type ‘(req: Request, res: Response, next: NextFunction) => void’.

Author: Fantashit

2 thoughts on “Express Typings Broken in TS 2.2.x (VS Code, WebStorm, pretty much everything)

  1. I’ve been using express typings with a number of projects without any problems. It seems to be me like the code in the initial post is missing 1) importing Request and Response and 2) are importing NextFunction from the wrong package.

    This is what have been working for me:

    // Import types
    import { Request, Response, NextFunction } from 'express'
    
    // Import express
    import express = require('express')
    
    // Create app
    const app = express()
    
    // Define route
    app.get('/', (req: Request, res: Response, next: NextFunction) => {
      res.send('Hello, World!')
    })
    
    // Listen for connections
    app.listen(3000, () => {
      console.log('http://localhost:3000')
    })

    edit: this is with npm install --save express @types/express

Comments are closed.