flow--JS静态类型检查

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值