一. 其他
1. 指定具体值
只能接受指定的值
function acceptsTwo(value: 2) {
// ...
}
acceptsTwo(2); // Works!
// acceptsTwo(3); // Error!
// acceptsTwo("2"); // Error!
function getColor(name: "success" | "warning" | "danger") {
switch (name) {
case "success" : return "green";
case "warning" : return "yellow";
case "danger" : return "red";
}
}
getColor("success"); // Works!
getColor("danger"); // Works!
// getColor("error"); // Error!
2. 混合类型
即各种类型的值都能接受
function stringify(value: mixed) {
// ...
}
stringify("foo");
stringify(3.14);
stringify(null);
stringify(undefined);
stringify({});
3. 任意类型
一般情况下是不推荐使用的
function add(one: any, two: any): number {
return one + two;
}
add(1, 2); // Works.
add("1", "2"); // Works.
add({}, []); // Works.
4. 可能值(Maybe types)
语法: ?number , 值可能是number, null, undefined
function acceptsMaybeNumber(value: ?number) {
// ...
}
acceptsMaybeNumber(42); // Works!
acceptsMaybeNumber(); // Works!
acceptsMaybeNumber(undefined); // Works!
acceptsMaybeNumber(null); // Works!
// acceptsMaybeNumber("42"); // Error!
acceptsMaybeNumber(Number('123')); // Works!
5. JavaScript变量声明
var let const
const foo /* : number */ = 1;
const bar: number = 2;
var barVar: number = 2;
let barLet: number = 2;
let isOneOf: number | boolean | string = foo; // Works!
let bool = true;
let isBoolean: boolean = bool; // Works!
let test: boolean = 42;
function mutate() {
test = true;
test = "hello";
}
mutate(); // Error!
vue源码学习 --- flow学习(3)
https://www.jianshu.com/p/440d52a7ef97