express icon indicating copy to clipboard operation
express copied to clipboard

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

Open joefallon opened this issue 9 years ago • 16 comments

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

joefallon avatar Mar 28 '17 21:03 joefallon

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?

dougwilson avatar Mar 28 '17 21:03 dougwilson

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

joefallon avatar Mar 28 '17 22:03 joefallon

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?

dougwilson avatar Mar 28 '17 22:03 dougwilson

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

joefallon avatar Mar 28 '17 23:03 joefallon

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

blakeembrey avatar Mar 28 '17 23:03 blakeembrey

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

natejgardner avatar Aug 04 '17 21:08 natejgardner

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

blakeembrey avatar Aug 05 '17 01:08 blakeembrey

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

blakeembrey avatar Aug 05 '17 01:08 blakeembrey

@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).

dougwilson avatar Aug 05 '17 01:08 dougwilson

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)

felixfbecker avatar Aug 05 '17 03:08 felixfbecker

Any news on this?

cristophersfr avatar Sep 19 '17 22:09 cristophersfr

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.

dougwilson avatar Sep 25 '17 06:09 dougwilson

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

LinusU avatar Sep 25 '17 08:09 LinusU

@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 avatar Dec 14 '17 07:12 micksatana

@micksatana Thanks! :)

joefallon avatar Dec 14 '17 16:12 joefallon

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.

wesleytodd avatar Mar 15 '18 23:03 wesleytodd