1.全局安装flow-bin: npm install --global flow-bin
2.切换到工作目录,初始化flow: flow init
,它会在当前目录下生成一个.flowconfig文件,内容如下
[ignore]
[include]
[libs]
[lints]
[options]
[strict]
3.flow文件就是将// @flow或 /* @flow */加到js文件的最顶部。只有flow文件,flow进程才会在后台监视这些文件,当有类型检查时,有错误它就会报错
4.新建testFlow.js文件进行测试
测试error案例一:
// @flow
function square(n:number): number {
return n * n;
}
square('2')
执行flow check,结果如下:
D:\projects\flow-bin>flow check
Found 0 errors
D:\cc\projects\flow-bin>
D:\cc\projects\flow-bin>flow check
Error -------------------------------------------------------------------------------------------------- testFlow.js:6:8
Cannot call `square` with `'2'` bound to `n` because string [1] is incompatible with number [2].
testFlow.js:6:8
6| square('2')
^^^ [1]
References:
testFlow.js:2:19
2| function square(n:number): number {
^^^^^^ [2]
Found 1 error
测试no error案例二:
// @flow
function concat(a: string, b: string) {
return a + b;
}
concat("A", "B"); // Works!
执行flow check,执行结果如下:
D:\cc\projects\flow-bin>flow check
Found 0 errors