Our Experiences with Flow
09 Dec 2015We’ve spent the past year building pileup.js, an interactive JavaScript genome browser. We’ve used Facebook’s Flow system throughout the development process to bring the benefits of static type analysis to our code. This post describes our experiences and provides some advice for getting the most benefit out of Flow.
What are the benefits of using Flow?
The most often-cited rationales for type systems are that they:
- Catch errors early
- Improve readability of code
- Facilitate tooling
- Improve runtime performance
In the case of Flow, runtime performance is not in play. The other factors are the main motivators.
For catching errors early, Flow winds up being a great way to avoid round-trips through the “save and reload in the browser” cycle. If you use Flow throughout your code, you’ll see far fewer Syntax Errors
or undefined is not a function
messages pop up in your JavaScript console. Similarly, it’s a huge help in refactoring. If you rename a module or function, Flow will immediately find all the references to the old name.
That being said, this aspect of Flow has proven to be mostly a time saver, rather than an effective way to find bugs. Most egregious bugs would have been caught by a unit test or manual testing before they were ever committed to source control. The type system just lets us find them more quickly.
The primary long-term reason to use Flow (or TypeScript) is for the improved readability of code. Far too often in JavaScript (or Python), you’ll run into a function like this:
function updateUI(data) {
...
}
It was surely obvious to whomever wrote the function (perhaps you, six months ago!) what data
was. You’d sure like to update the UI. But what exactly do you pass to this function? For simple code, you may be able to infer the parameter type by inspection. But for complex functions which call out to other functions in other modules, this quickly becomes difficult. Type annotations and type inference can both immediately tell you what’s expected.
For large codebases, this winds up being a huge aid. Complex UIs often use large, nested objects to represent their state. This is very common with React Components, for example. With type annotations, it’s clear what type of object each component expects. And, if you’re still confused, Flow will let you know. What’s more, unlike types which are documented in comments, you can be confident that type annotations are up-to-date and correct. If they weren’t, Flow would have complained.
Why use Flow instead of TypeScript?
An aside: why did we use Flow instead of TypeScript? When we started work on pileup.js, TypeScript had poor support for both Node-style require
statements and for React.js and its JSX syntax. These issues are both less relevant now: both Flow and TS support ES6 import
statements, which is what all projects should be using going forward. TypeScript 1.6 added support for JSX, so that’s less relevant too.
Both Flow and TypeScript are good choices for a new project. Flow is a more sophisticated type system, with better support for patterns like type unions and nullable types. TypeScript is more established and has better support, e.g. more editor plugins and third-party type definitions.
Practical experiences / gotchas
Editor integration
While you can run flow check
and flow status
from the command line and as part of your continuous integration, you’ll be missing out on many of its benefits if this is all you do. Flow works best when it integrates with your editor. With the vim-flow plugin, for example, Flow will show any new errors inside of vim whenever you save a JS file. This is fast (or should be – see below!) and helps you catch errors without the context switch of going to your web browser.
Flow is a server
Flow is intended to be run as a background server. When you run flow status
, it starts up the server, reads and analyzes all your code and then waits for files to change. When you run flow status
again, you’re just asking the server for the latest errors. Incremental changes are fast, but the server startup is slow. flow check
runs a full check and is always slow. So use flow status
, except in contexts which are not latency-sensitive, e.g. your continuous integration build.
Sometimes you’ll make a change that forces the Flow server to restart. These include changes to your .flowconfig
file or changes to a type declaration (lib) file. There’s no way around this. You’ll just have to wait 20-30 seconds to get type errors again.
Flow reads your entire node_modules directory
Incremental compiles should take under a second. You can check this time for your project by running:
echo '' >> some/js/file.js; time flow status
If this time gets too large (e.g. more than a second), it might be because Flow is wasting time reading everything under node_modules
. You can check how many files Flow is tracking by running this sequence:
flow check --verbose 2> /tmp/flow-all.txt
grep -o '/node_modules.*' /tmp/flow-all.txt | perl -pe 's/:.*//; s/[\\]?".*//; s/ .*//' | sort | uniq | wc -l
If this is significantly larger than the number of JS files in your project, then Flow is wasting time. You should add as much of node_modules
as you can to the [ignore]
section of your .flowconfig
. See this issue for details.
Creeping any
types
One of the pitches for Flow is that it performs type inference on your JavaScript. You don’t even have to provide type annotations, Flow will figure out your code! For a few reasons, however, this does not work in practice. First, Flow requires that you explicitly annotate all functions and classes which are exported from modules (this is for performance reasons). Secondly, there are many ways that any types can creep into Flow code:
- Third-party libraries If you import a third-party module (e.g. underscore), then the type of the entire module will be
any
. This means that any data that passes through functions in that module will come out with anany
type. The solution to this is to use a type declaration file. While there is some movement towards providing a central registry of Flow type definitions, it’s nowhere near as mature as TypeScript’s DefinitelyTyped. -
Props and State If you use React, you need to explicitly specify types for your
props
andstate
. Otherwise, they default toany
. You can get slightly better behavior by usingpropTypes
, which Flow understands. But the best way is to explicitly define types forprops
andstate
using an ES6 class:type Props = { name: string; // note: not null! address: string; }; class AddressBookEntry extends React.Component<void /* DefaultProps */, Props, void /* state -- none in this component */> { ... }
This will check types for
props
,state
andsetState
within your component. It will also check theprops
onAddressBookEntry
when you create one using JSX syntax elsewhere in your code (i.e.<AddressBookEntry name="Dan" />
would be an error becauseaddress
is missing). If you defineprops
andstate
members, you will get the former but not the latter check. - Missing @flow annotations If you forget to put
@flow
at the top of your source file, Flow will ignore it by making its typeany
. We added a lint check to ensure that all our JS files had this annotation.
You can run flow coverage path/to/file.js
to see the fraction of expressions in a file whose type is any
. You should try to keep this as low as possible. The :FlowType
vim command shows you what Flow thinks the type of the expression under your cursor is. This can also help to find gaps in what Flow will catch.
Surprising non-errors
JavaScript is a very permissive language. Flow models some elements of this permissiveness but disallows others. For example, '1' + 1
evaluates to '11'
. Flow complains about this because it’s likely to be a mistake (and at the very least indicates unclear thinking).
JavaScript also allows you to pass any number of arguments to functions. For example, Math.pow(2, 4, 6, 8) == 16
. Arguments after the second are simply ignored. This is also likely to be a mistake, but Flow is OK with passing too many arguments. Other examples include functions are objects.
We often find ourselves wondering “would Flow catch this?” It’s a good habit to introduce errors and typos to double-check that Flow is understanding your code.
Misattributed / cryptic errors
Many students have a sinking realization during their Compilers classes that, while parsing a correctly-formed program is fun and easy, 95% of the challenge in writing a good parser is the tedious process of catching errors, determining where they are, and recovering from them.
Something similar seems to be at play with Flow. If Flow reports that there are no errors, then (modulo any
types and non-errors, see above) you can be confident that there are none. If it reports a problem, then there probably is one. But the error message may or may not point you in the right direction. There are many open issues around error messages.
For example, we’ve learned from experience that intersection types can be a source of cryptic errors. For example:
var el = document.createElement('canvas');
var ctx = el.getContext('2d');
ctx.fillRect(0, 0, 200, 100);
This produces an odd-looking error:
2: var ctx = el.getContext('2d');
^^^^^^^^^^^^^^^^^^^ call of method `getContext`. Function cannot be called on
2: var ctx = el.getContext('2d');
^^^^^^^^^^^^^^^^^^^ HTMLCanvasElement
getContext
is most certainly a method on HTMLCanvasElement
. The real issue is on the third line, not the second. getContext
is allowed to return null
, which does not have a fillRect
method. This error bubbles up to the point where these two cases diverge, resulting in the cryptic message. The solution is to add an if (ctx) { ... }
check around the drawing code.
This example illustrates both the strengths and weaknesses of Flow. It’s impressive that Flow can track the way that functions return different types based on parameter values, e.g. HTMLCanvasElement
from document.createElement('canvas')
. This eliminates a common source of typecasts in other type systems. It also illustrates a case where null-checking is not helpful. In practice, getContext('2d')
will not return null
.
Conclusions
Flow is a powerful tool for performing static type analysis on JavaScript codebases, bringing their maintainers many of the benefits that this yields. Flow is not without its quirks, though, some of which we’ve documented here. Understanding these will help you get the most benefit out of Flow.
Flow is developing rapidly, with dozens of commits being merged every week. This is one of the very best things about it. In the year that we’ve been using it, many new features have been added and many fundamental issues have been resolved. It’s likely (we hope!) that some of the gotchas in this post will no longer be gotchas by the time you read it.