javascript 是动态类型的代码,有很多的写法很不容易报错,想引入静态类型检查的flow,之前做项目的时候用的typascript的,看的大概的写法和 typescript 类似,因为规范避免了些低级错误
1 ,使用flow 的静态检查的首先在文件开头加上 /* @flow*/,
2 ,静态检查有2种方式
1,根据上下文推断 上下该方法返回的是什么类型
/* @flow*/
function split( str) {
return str. split( ' ');
}
split( 11); //提示错误
2, 规定的函数、变量是什么类型
function addClass( a: number, b: number) {
return a+ b
}
addClass( 1, 4)
还有静态的变量
var arr: Array< number>=[ 1, 2, 3]
类的定义
class Bar{
x: number;
y: string;
z: boolean
constructor( x: number, y: string){
this. x= x;
this. y= y;
this. z= false;
}
}
flow具有自定义库里面的类型的集合,查看flow 的文件的拆分
pasting
flow ----- compiler //定义编译文件中全局的定义
----- component //组件数据结构内的变量
----- global- api //全局变量的接口定义
----- ssr //服务端渲染的接口定义
----- vnode //虚拟node相关
----- options //选项相关
----- module. js // 第三方库相关