Express' Request and Response types don't work for middlewares or routes.
See original GitHub issueI’m trying to type express middlewares, but if I omit the type signature they end up being any:
/* @flow weak */
const express = require('express')
const router = new express.Router()
router.get('/', function (req, res) {
})
$ flow type-at-pos router.js 6 28
any
/home/despairblue/git/wunderflats/api/router.js:6:27,6:29
And annotating them using the Request and Response classes from the express libdef leads to this:
/* @flow weak */
const express = require('express')
const router = new express.Router()
router.get('/', function (req /* : Request */, res /* : Response */) {
})
router.js:6
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^ call of method `get`
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^ call of method `get`. Function cannot be called on any member of intersection type
92: (middleware: Middleware): T;
^^^^^^^^^^^^^^^^^^^^^^^^^^^ intersection. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:92
Member 1:
92: (middleware: Middleware): T;
^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:92
Error:
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^^^ string. This type is incompatible with
92: (middleware: Middleware): T;
^^^^^^^^^^ union: function type(s). See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:92
Member 1:
89: ((req: Request, res: Response, next: NextFunction) => mixed) |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:89
Error:
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^^^ string. This type is incompatible with
89: ((req: Request, res: Response, next: NextFunction) => mixed) |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:89
Member 2:
90: ((error: ?Error, req : Request, res: Response, next: NextFunction) => mixed);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:90
Error:
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^^^ string. This type is incompatible with
90: ((error: ?Error, req : Request, res: Response, next: NextFunction) => mixed);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:90
Member 2:
93: (...middleware: Array<Middleware>): T;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:93
Error:
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^^^ string. This type is incompatible with
93: (...middleware: Array<Middleware>): T;
^^^^^^^^^^ union: function type(s). See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:93
Member 1:
89: ((req: Request, res: Response, next: NextFunction) => mixed) |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:89
Error:
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^^^ string. This type is incompatible with
89: ((req: Request, res: Response, next: NextFunction) => mixed) |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:89
Member 2:
90: ((error: ?Error, req : Request, res: Response, next: NextFunction) => mixed);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:90
Error:
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^^^ string. This type is incompatible with
90: ((error: ?Error, req : Request, res: Response, next: NextFunction) => mixed);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:90
Member 3:
94: (path: string|RegExp, ...middleware: Array<Middleware>): T;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:94
Error:
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^ function. This type is incompatible with
94: (path: string|RegExp, ...middleware: Array<Middleware>): T;
^^^^^^^^^^ union: function type(s). See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:94
Member 1:
89: ((req: Request, res: Response, next: NextFunction) => mixed) |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:89
Error:
89: ((req: Request, res: Response, next: NextFunction) => mixed) |
^^^^^^^ Request. This type is incompatible with. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:89
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^^^^^^^ Request
Member 2:
90: ((error: ?Error, req : Request, res: Response, next: NextFunction) => mixed);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function type. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:90
Error:
6: router.get('/', function (req /* : Request */, res /* : Response */) {
^^^^^^^ Request. This type is incompatible with
90: ((error: ?Error, req : Request, res: Response, next: NextFunction) => mixed);
^^^^^ null. See lib: flow-typed/express_v4.x.x/flow_>=v0.25.x/express_v4.x.x.js:90
Versions:
- flow: 0.30.0
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Complete Guide to Express Middleware - Reflectoring
Middleware in Express are functions that come into play after the server receives the request and before the response is sent to the...
Read more >Using middleware - Express.js
An Express application can use the following types of middleware: Application-level middleware.
Read more >node.js - Request doesn't go through the middleware in express
I am integrating a Twitch API for subs, and having issue with getting the webhook callback response to the middleware function ...
Read more >Express Tutorial Part 4: Routes and controllers - MDN Web Docs
Note: Router functions are Express middleware, which means that they must either complete (respond to) the request or call the next function ...
Read more >Using Express.js Routes for Promise-based Error Handling
use(function(err, req, res, next) { res.promise(Promise.reject(err)); });. Note that we do not omit our error middleware. It's still an important error handler ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Without changing the libdef, the following is working for me on flow 0.38.0:
or
I have a different set of issues when I try it out that come down to the definition of
Middleware
It seems that it’s using a union type (
A | B
) instead of an intersection type (A & B
).However I don’t know where in the express api you have this.
Changing the definition of Middleware to the following seems to fix everything:
[try]