Flow is a JavaScript static type checker built by
14000+ stars on
Main features:
var myObject = {
prop1: 'Some text here'
}
console.log(myObject.prop1());
// TypeError: myObject.prop1 is not a function
There is no compile error !!!
// implicit type coercion in JavaScript
var myNumber = 20;
console.log(typeof myNumber); // number
myNumber = true;
console.log(typeof myNumber); // boolean
var obj = {
data: {
getListingData: function(){};
},
map: "some map",
.
.
.
};
//In Another file
function func(obj) {
obj.data.getListingData();
};
yarn add --dev babel-cli babel-preset-flow
{
"presets": ["flow"]
}
yarn add --dev flow-bin
flow init
[ignore]
.*/build/.*
.*/node_modules/.*
[include]
[options]
....
.flowconfig file
// @flow
or
/* @flow */
yarn run flow
yarn run flow status
yarn run flow stop
yarn run v1.3.2
$ flow
No errors!
✨ Done in 0.17s.
function acceptsMaybeNumber(value: ?number) {
// ...
}
acceptsMaybeNumber(42); // Works!
acceptsMaybeNumber(); // Works!
acceptsMaybeNumber(null); //works
// @flow
function concat(a: string,b: string):string{
return a + b;
}
concat("foo", "bar"); // Works!
// $ExpectError
concat(true, false); // Error!
// @flow
type MyObject = {
foo: number,
bar: boolean,
baz: string,
};
var val: MyObject = {
foo: 1,
bar: true,
baz: 'three',
};
// @flow
class MyClass {
prop: number;
method() {
this.prop = 42;
// $ExpectError
this.data = "blala"; // Error!
}
}
import * as React from 'react';
type Props = {
foo: number,
bar?: string,
};
function MyComponent(props: Props) {
props.doesNotExist;
// Error!You did not define a `doesNotExist` prop.
return <div>{props.bar}</div>;
}
<MyComponent foo={42} />
Type checker +
A Complier that emits plain JavaScript
Angular Community
Type checker
Rely on Babel or other tools to remove type annotation
React Community
Hey we’d like to use this library to improve code quality on your project
vs
We’d like to write the code for your project in a language your internal team doesn’t know.