question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Express' Request and Response types don't work for middlewares or routes.

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

21reactions
shuheicommented, Feb 1, 2017

Without changing the libdef, the following is working for me on flow 0.38.0:

router.get('/', function (req: express$Request, res) {
})

or

import type { $Request } from 'express';

router.get('/', function (req: $Request, res) {
})
3reactions
jamiebuildscommented, Sep 12, 2016

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:

declare type Middleware = ((req: Request, res: Response, next: NextFunction) => mixed);

[try]

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found