Skip to content

Instantly share code, notes, and snippets.

@shanecav
Created July 19, 2016 02:43
Show Gist options
  • Save shanecav/796fa6e4b93ee0ffcd882b039090627c to your computer and use it in GitHub Desktop.
Save shanecav/796fa6e4b93ee0ffcd882b039090627c to your computer and use it in GitHub Desktop.
Flowtype error on node http Server listen method
const createServer = require('http').createServer
const app = require('./http/app') // Express app
const server = createServer(app)
server.listen(port)
/**
* ^ This is where I'm getting the Flow error:
*
* call of method `listen`: /Users/shane/projects/blockpop/server/src/server.js:22
* Function cannot be called on any member of intersection type
* intersection: /Users/shane/projects/blockpop/server/src/server.js:22
* Member 1:
* function type: /private/tmp/flow/flowlib_3f09764c/node.js:696
* Error:
* boolean: /Users/shane/projects/blockpop/server/src/server.js:22
* This type is incompatible with
* number: /private/tmp/flow/flowlib_3f09764c/node.js:696
* Member 2:
* function type: /private/tmp/flow/flowlib_3f09764c/node.js:697
* Error:
* boolean: /Users/shane/projects/blockpop/server/src/server.js:22
* This type is incompatible with
* string: /private/tmp/flow/flowlib_3f09764c/node.js:697
* Member 3:
* function type: /private/tmp/flow/flowlib_3f09764c/node.js:698
* Error:
* boolean: /Users/shane/projects/blockpop/server/src/server.js:22
* This type is incompatible with
* object type: /private/tmp/flow/flowlib_3f09764c/node.js:698
*/
// Here's the http declaration Flow is using, which is referenced in the Flow error above:
declare module "http" {
declare class Server extends net$Server {
listen(port: number, hostname?: string, backlog?: number, callback?: Function): Server; // line 696
listen(path: string, callback?: Function): Server; // line 697
listen(handle: Object, callback?: Function): Server; // line 698
close(callback?: Function): Server;
maxHeadersCount: number;
setTimeout(msecs: number, callback: Function): Server;
timeout: number;
}
declare class ClientRequest extends http$ClientRequest {}
declare class IncomingMessage extends http$IncomingMessage {}
declare function createServer(listener?: Function): Server;
declare function request(
options: Object | string,
callback?: (response: IncomingMessage) => void
): ClientRequest;
declare function get(
options: Object | string,
callback?: (response: IncomingMessage) => void
): ClientRequest;
}
@samwgoldman
Copy link

Flow is inferring the type of port to be boolean. The error shows that there are 3 possible ways to call listen, one that takes a number, one a string, and one an Object—and boolean is incompatible with all of those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment