认识 Flow
1 认识Flow
2.为什么用Flow
3.Flow的工作方式
通常,类型检查分为2种方式:
3.1类型判断
3.1类型注释
/*@flow*/ function add(x, y){ return x + y } add('Hello', 11)
/*@flow*/ function add(x: number, y: number): number { return x + y } add('Hello', 11)
/*@flow注释数组*/ var arr: Array<number> = [1, 2, 3] arr.push('Hello')
/*@flow注释类和对象*/ class Bar { x: string; // x 是字符串 y: string | number; // y 可以是字符串或者数字 z: boolean; constructor(x: string, y: string | number) { this.x = x this.y = y this.z = false } } var bar: Bar = new Bar('hello', 4) var obj: { a: string, b: number, c: Array<string>, d: Bar } = { a: 'hello', b: 11, c: ['hello', 'world'], d: new Bar('hello', 3) }
/*@flow*/ var foo: ?string = null
4.Flow在Vue.js源码中的应用
总结