Flow
基本类型的类型标注语法
-
JavaScript 中的基本类型,类型标注语法是在变量后加上一个冒号,空格,然后是相应的类型名称
-
undefined 的类型是 void; null 的类型是 null;string 类型、number 类型和 boolean 类型,其类型名称都是小写开头
const a: string = 'a'; // 字面量值对应的类型名称是小写 const b: string = String('b'); // String 函数是将参数转化成一个字符串,仍然是小写的 string 类型 const c: String = new String(‘c’); // 大写开头的类型名称,其对应的值是 new 创建出来的类型实例;
-
字面量值也可以作为一种类型,符合这种类型的变量只有这个字面量本身
// @flow
let monthsAYear: 12 = 12;
monthsAYear = 13; // Flow 会在这里报错
函数类型标注方法
// 函数声明
function getLength(str: string): number {
return str.length;
}
// 函数表达式
const greeting = function(welcome: string): void{
console.log(welcome);
}