When Flow typing didn’t quite work properly (and it was all my own fault)
I’m working on a Javascript and React application, which uses Flow to enforce types. For the most part, I love working with Flow, but it hadn’t been working properly for a while.
My IDEs (both Webstorm and VSCode) both reported type errors correctly, but running flow check
at the command line (or on CircleCI) always returned No errors!
Great, except there were errors lurking in there – loads more than Webstorm was finding!
It turned out to be because I had this line in my .flowconfig
:
[options]
module.system.node.resolve_dirname=src
That was there so I could use absolute imports (meaning I could type import Foo from "utils/bar";
instead of import Foo from "../../../../../../../../utils/bar";
). Unfortunately that turned out to a broken config, which masked a hell of a lot of problems! Luckily I wasn’t the first person to run into this. The correct way to do that is:
[options]
module.name_mapper='^utils/\(.*\)$' -> '<PROJECT_ROOT>/src/utils/\1'
And add another module.name_mapper
line for each top-level folder under src
.
If you’re importing from a file at the top level (e.g. import type { Foo } from "flow-types";
) the module.name_mapper
will look something like this:
module.name_mapper='^flow-types' -> '<PROJECT_ROOT>/src/flow-types.js'
Unfortunately, when I corrected the problem, it revealed a lot of Flow errors in my codebase. Flow has got a lot more strict since I introduced the problem (and that’s a good thing). Luckily, most of those issues are relatively simple to fix!