关于 Flow

Flow 一个 JAVASCRIPT 静态类型检测器

创建 .flowconfig 或者通过全局安装 flow,利用flow init 在项目的根目录 进行配置的初始化 (一般不需要进行额外的修改) 

默认检测 统计目录下的所有文件

[include] : 需要检测的目录

[ignore] : 不需要检测的目录

[lib] : null , [options] : null

[version] : flow 版本 

 

如果要flow 检测js文件 需要在函数头部 添加注解 // @flow 或者 /* @flow */

例如 : 

// @flow

function a(str:string){

  return str.length

}

a(null) 

Error: index.js:6
6: a(null);
^^^^ null. This type is incompatible with the expected param type of
2: function a(name: string) {
^^^^^^ string

 

flow:支持的类型:

原声的javascript类型 : string,boolean,number,void(undefined),null

拓展类型 :

any(所有类型都可以,相当于不检测),

mixed(动态数据类型),

Array<T> 来声明一个数组后(var arr:Array<string>),

Objects (let object:{a:number,b:string} = {a:1,b:'string'}),

callable:null,

Object:所有对象的超集(和any类似 ),

Class<T>(范型类)

 

JavaScript 既是弱类型语言又是动态类型语言,极其容易出错,也是它成为糟糕语言的一个重要原因。

由于前期成本很低,并且具有缓慢演进的能力,Flow 通过向 JavaScript 添加类型系统来解决这两个问题。

好处详见:  http://www.zcfy.cc/article/why-use-flow-1585.html

 

原文地址:https://www.cnblogs.com/mooniitt/p/8269993.html