一.安装flow-bin

二、原始类型 string number boolean null undefined (这里用的void) symbol

三.数组类型

四.对象类型
属性后面❓,意思可以定义可以不定义,可以是其他类型,就是不能为null

五.函数参数类型,返回值类型
一般写在fun():string{}意思返回值要为string,回调函数里面注解看下图

六.特殊类型
类型前面❓,意思还可以是undefined或者null

七.mixed any

//@flow
function sum(a: number, b: number) {
return a + b;
}
sum(100, 100);
const a: string = 'qw';
const b: boolean = true;
const c: null = null;
const d: void = undefined;
const e: number = 0;
const f: symbol = Symbol();
const arr1: Array<number> = [1, 2, 3];
const arr2: number[] = [1, 2, 3, 4];
const arr3: [string, string, number] = ['1', '2', 2];
const obj1: { a: number, b: string } = { a: 1, b: '2' };
const obj2: { a?: number, b: string } = { b: '2' };
const obj3: { [string]: string } = {};
obj3.foo = 'foo';
obj3.bar = 'bar';
function getData(a:string) :void{
//void就是无返回值,或者为undefined
}
getData('12');
function foo(callback: (string, number) => string) {
callback('string',120);
}
foo(function (string, number) { return 'haha' });
const as: string | boolean | number = false;
type _twoType = string | number;//type定义好备用
const des: _twoType = 100;
const ert: ?number = null//2//undefined
const element: HTMLElement | null = document.getElementById('app');
网友评论