Flow

Developed and maintained by Facebook, Flow is a static type checker for JavaScript. It is meant to support React.

To install Flow:

npm install --save-dev flow-bin flow-remove-types

Add to 'package.json':

{ "scripts": { "flow": "flow",
"build": "flow-remove-types -d lib/ src/", },}

Then initialize Flow:

npm run flow init

If your project was set up using Create React App, the Flow annotations are already being stripped by default so you don't need to do anything else.To start the Flow server, which checks for changes in .js files with the //@flow annotation in the current directory and its subdirectories:

npm run flow

To stop the server:

npm run flow stop

To strip the type annotations from these Flow .js files:

npm run flow build

An explanation of all the possible Flow syntaxes is beyond the scope of this book:


RESETRUNFULL
type GreetingProps = { name: string };class Greeting extends React.Component<GreetingProps, void> { constructor(props: GreetingProps) {
  super(props);  // some initialisation stuff } render() {
  const {name} = this.props;
  return (<div>{name}</div>);
  }}

Using data flow analysis, Flow automatically infers types and tracks data as it moves through your code. You don't need to fully annotate your code before Flow can start to find bugs:


RESETRUNFULL
// @flowfunction square(n) {
  return n * n; // Error!}square("2");