FLOW IS A STATIC TYPE CHECKER FOR JAVASCRIPT.
node
安裝完成後執行 flow init 會自動生成一個 .flowconfig 文件,這個文件告訴 Flow 須要檢測根目錄以及全部子目錄的代碼。(.flowconfig 有一些高級配置在下面的章節會講到。)react
$> mkdir new_project
$> cd new_project
$> flow init
|
有了配置文件,咱們就能夠使用下面的指令檢測代碼了:git
$> flow check
# 全量檢查
$> flow
# 開啓一個後臺服務,輸出首次檢測結果,當發生修改時,再次運行 flow 會增量地檢查文件
$> flow stop
# 代碼寫完,關閉服務
|
對於須要使用 flow 進行類型檢查的 js 文件,在開頭加入 @flow 註釋。github
/* @flow */
/* @flow weak */
只對有加類型註解的變量進行類型檢測
|
無論有沒有 @flow 註釋,均可以使用 flow check --all 強制檢查全部文件。web
function concat(a, b) {
return a + b;
}
concat("A", "B"); // Works!
concat(1, 2); // Works!
// @flow
function concat(a: string, b: string) {
return a + b;
}
concat("A", "B"); // Works!
concat(1, 2); // Error!
|
null
undefined
(void
in Flow types)
// @flow
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!
// $ExpectError
getColor("error"); // Error!
|
function stringifyBasicValue(value: string | number | boolean) {
return '' + value;
}
|
應該儘量避免,可是在作項目遷移時,能夠使用 any 作平穩過渡。——能用 mixed 就不用 any。typescript
// @flow
function acceptsMaybeNumber(value: ?number) {
// ...
}
acceptsMaybeNumber(42); // Works!
acceptsMaybeNumber(); // Works!
acceptsMaybeNumber(undefined); // Works!
acceptsMaybeNumber(null); // Works!
acceptsMaybeNumber("42");
|
var
varVariable = 1;
let letVariable = 1;
const constVariable = 1;
varVariable = 2;
// Works!
letVariable = 2;
// Works!
// $ExpectError
constVariable = 2;
// Error!
|
主要概念:參數、函數返回值、可選參數、rest 參數。npm
let method = (str: string, bool?: boolean, ...nums: Array<number>): void => {
// ...
};
|
// @flow
var obj = { foo: "bar" };
// $ExpectError
obj.bar; // Error!(not undefined)
|
sealed object 不容許被添加新屬性。redux
// @flow
var obj = {
foo: 1
};
// $ExpectError
obj.bar = true; // Error!
// $ExpectError
obj.baz = 'three'; // Error!
|
若是對象在建立時沒有任何屬性,那麼它就是 unsealed object,容許添加屬性。若是 flow 能推斷出這個屬性的類型,就給他指明類型,不然,給他全部可能的類型。vim
// @flow
var obj = {};
obj.foo = 1; // Works!
obj.bar = true; // Works!
obj.baz = 'three'; // Works!
// @flow
var obj = {};
obj.prop = true;
obj.prop = "hello";
// $ExpectError
var val1: boolean = obj.prop; // Error!
var val2: string = obj.prop; // Works!
// @flow
var obj = {};
if (Math.random()) obj.prop = true;
else obj.prop = "hello";
// $ExpectError
var val1: boolean = obj.prop; // Error!
// $ExpectError
var val2: string = obj.prop; // Error!
var val3: boolean | string = obj.prop; // Works!
|
let arr1: Array<boolean> = [true, false, true];
let arr2: Array<string> = ["A", "B", "C"];
let arr3: Array<mixed> = [1, true, "three"];
|
除了上面介紹的類型,還有不少其餘的 types,能夠直接去官網查看。sublime-text
$> npm clone https:
//github.com/facebook/flow.git
$> cd flow/examlpes
$> flow
|
相似npm
$> flow --help
$> flow cmd --help
|
[include]
../externalFile.js
../externalDir/
[ignore]
.*/build/.*
[libs]
./lib
[options]
module.system.node.resolve_dirname=node_modules
module.use_strict=true
[version]
0.22.0
|
通常來講,咱們不想去檢測第三方模塊的代碼,官方推薦的作法是使用 flow-typed。
若是在 flow-typed 中找不到定義好的模塊,也能夠本身本身寫。
// Declaring A Global Function
declare function foo(a: number): string;
// Declaring A Global Class
declare class URL {
constructor(urlStr: string): URL;
toString(): string;
static compare(url1: URL, url2: URL): boolean;
};
// Declaring A Global Variable
declare var PI: number;
// Declaring A Global Type
declare type UserID = number;
// Declaring A Module
declare module "some-third-party-library" {
// This is where we'll list the module's exported interface(s)
}
// Declaring An ES Module
declare module "some-es-module" {
// Declares a named "concatPath" export
declare export function concatPath(dirA: string, dirB: string): string;
}
// Declaring A CommonJS Module
declare module "some-commonjs-module" {
// The export of this module is an object with a "concatPath" method
declare module.exports: {
concatPath(dirA: string, dirB: string): string;
};
}
|
$> npm install --save-dev babel-preset-flow
|
{
"presets": ["flow"]
}
|
|
flow
|
typescript
|
---|---|---|
slogan | A static type checker for JavaScript | JavaScript that scales |
開發者 | 微軟 | |
相關技術 | React | Angular |
star | 1w+ | 2w+ |
文檔 | 少 | 多 |
第三方庫支持 | 弱 (flow-typed) | 強 (tsd) |
IDE支持 | 弱 | 強 |
其餘 | 自由度高,老項目遷移比較方便 | 工程化強,適合新項目、大項目 |