⬆️ ⬇️

Flow - static type analysis in JS from Facebook

Flow is a static code analyzer and a set of syntax structures for directly specifying the type of variable.

Flow can calculate the type of a variable, without making changes to the code (unlike TypeScript), which allows you to start using it now in any project. It is also possible to independently specify types in the TypeScript style.



There are 3 modes:

  1. Do not check anything by default
  2. Check without using annotations (with comment annotation, as in React)
  3. Strict indication of the type of variable (with a change directly to the code)




/* @flow */ function foo(x) { return x * 10; } foo('Hello, world!'); 


 $> flow hello.js:5:5,19: string This type is incompatible with hello.js:3:10,15: number 




In this case, the analyzer independently derived the type of the variable, but you can also prompt:

')

 /* @flow */ function foo(x: string, y: number): string { return x.length * y; } foo('Hello', 42); 


 $> flow hello.js:3:10,21: number This type is incompatible with hello.js:2:37,42: string 




There is also a weak mode that allows you to check the code for errors that are easy to quickly fix:





Installation

Flow is written in OCaml (> = 4.01.0).

There are binary builds for OSX and Linux , Windows is not supported.

For those who write on OCaml, you can use the OPAM package manager:

 opam install flowtype 


And OSX users have the opportunity to deliver via Brew:

 brew install flow 




Future plans





Links

Source: https://habr.com/ru/post/243713/



All Articles