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

Created on 28 Mar 2017  路  16Comments  路  Source: expressjs/express

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: https://github.com/Microsoft/TypeScript/issues/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'.

Most helpful comment

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

All 16 comments

Hi @joefallon we don't maintain any Typings, so there really is nothing we can address directly. Do you know who maintains the typings / where are they stored?

Oh goodness, no idea. @types/express I suppose.

Does https://github.com/Microsoft/TypeScript/issues/11155 sound like the same issue? If so, it seems they are saying it is a limitation of TS and not fixable? Even if it was, how would I go about fixing it? Where is the code and how do I write typescript?

That's a good question. I am clueless here. :)

@joefallon The definition comes from https://github.com/DefinitelyTyped/DefinitelyTyped, you should probably log an issue there. There's a better definition of Express.js written in http://github.com/types/npm-express, but I don't think it's compatible with the rest of TypeScript's @types work.

Did anyone get this resolved? Seems to still be broken.

@natejgardner We couldn't really answer, sorry - it's not maintained here. You'll have to ask the maintainers. We (@types) have written a separate definition that should work: https://github.com/types/npm-express.

@dougwilson I can contribute the typings we do have to Express.js and continue iterating if that works for you? /cc @felixfbecker

@blakeembrey SGTM. If we can get the ball rolling on it now, we can have a very nice reason to upgrade to 4.16 (including TSD).

Awesome. The typings depend on https://github.com/types/npm-serve-static, so I would propose to contribute them too. Then Express is completely TS compatible out of the box (path-to-regexp already has included typings)

Any news on this?

I'm happy to accept typings, but really don't know enough to create them myself. Ideally we could get them into the 4.16 release, but that is approaching quickly.

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

@joefallon I recently contribute to @types/express and quite sure this issue can be closed.

First, run npm install @types/express. You will have both @types/express and @types/express-serve-static-core installed.

And you can use either..

import * as express from 'express';

Or

import express = require('express');

Then follow by

import { Request, Response, NextFunction } from 'express-serve-static-core';

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

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

});

export = router;

@micksatana Thanks! :)

This is still open but has seen no activity since @micksatana said it can be closed. So I am going to close this, but on thing someone who has a stake in this should look at are the changes coming in 5.0 so that when we release that it does not break everyone if the interfaces change.

Was this page helpful?
0 / 5 - 0 ratings