Search code examples
flowtypeflow-typed

Flow-typed definitions being ignored in favor of npm module?


Consider the following Javascript and Flow code:

import type { $Request, $Response } from 'express';

function middleware(req: $Request, res: $Response) {}

middleware({}, {});

(full code at https://github.com/bradvogel/flow-playground)

When express isn't installed as an npm module, Flow correctly flags the code error:

enter image description here

However, when I npm install express, Flow can no longer resolve the types (from flow-typed):

enter image description here

Can someone explain who Flow is trying to import the types from the Express module, versus from flow-typed? How do I overcome this?


Solution

  • Flow doesn't really know about Node packages as a unit, so if you don't want Flow to try to parse things in node_modules, you'll want

    [ignore]
    <PROJECT_ROOT>/node_modules/.*
    

    in your .flowconfig. If you did want to allow a subset of node_modules, the [ignore] explain how to do that.

    I'm not aware of how Flow prioritizes explicitly-declared type definitions from flow-typed vs real files, but presumably given what we're seeing here, Flow must try to load type definitions from the actual imported file unless the file is ignored.