Flow can catch common bugs in JavaScript programs before they run, including:
- silent type conversions,
null
dereferences,- and the dreaded
undefined is not a function
.
1 2 3 4 5 |
// @flow function foo(x) { return x * 10; } foo('Hello, world!'); |
$> flow
3: return x * 10;
^ string. The operand of an arithmetic operation must be a number.
Flow also lets you gradually add type assertions to your code:
1 2 3 4 5 |
// @flow function bar(x): string { return x.length; } bar('Hello, world!'); |
$> flow
3: return x.length;
^^^^^^^^ number. This type is incompatible with the expected return type of
2: function bar(x): string {
^^^^^^ string
JavaScript code with Flow annotations easily transforms down to regular JavaScript, so it runs anywhere.