Flow是facebook出品的JS类型检查工具,他能够作类型检查,所谓的类型检查就是在编译期间今早发现由类型错误引发的bug,又不影响代码运行,使编写JS具备编写JAVA等强elixir语言相近的体验,因此vue使用flow作了静态检查。vue
Flow一般类型检查分红两种:数组
1.类型判断函数
2.类型注释工具
1、类型判断是经过变量的使用上下文来推断出变量类型,而后根据这些判断来检查类型(相对于在那写了个If),他不须要任何修改便可进行类型检查。this
例子:spa
/*@flow*/'' function aplit(str){ return str.split(''); } split(11); //这个会报错由于函数split期待的参数是字符串,咱们输入了数字
2、类型注释:在某种条件下添加类型注释能够更高明确的检查依据。code
例子:对象
/*@flow*/ function add(x:number,y:number):number{ return x+y } add('hello',11); //报错由于hello是字符串
常见的类型注释blog
数组:文档
例子
/*@flow*/ var arr:Array<number> = [1,2,3] arr.push('hello'); //报错
类和对象
例子
/*@flow*/ class Bar{ x:string; y:string | number; z:boolean;
constructor(x:string,y:string|number){
this.x = x;
this.y = y;
this.z = false
}
}
var bar:Bar = new Bar('hellow',4)
var obj:{ a:string, b:number, c:Array<string>,d:Bar} = {
a:'hello',
b:11,
c:['hello','world'],
d:new Bar('hello',3);
}
Null:若想任意类型能够为null或者是undefined,那么如例子所示就好了
例子
/*@flow*/ var foo:?string = null
若是想了解更多,那么能够访问flow官方文档。