记某年的一次团队分享,主要目的:优化又臭又长维护噩梦的JavaScript老项目html
JavaScript写起来,行云流水、挥洒自如、无拘无束、笔走龙蛇、随心所欲
react
金主粑粑,天天抓狂,小修小补的hotfix从未中止
,脆弱的代码
经不住半点风浪webpack
Flow是JavaScript代码的静态类型检查器
。 它能够帮助您提升工做效率
。 让您的代码更快,更智能,更自信,更大规模
。git
Flow经过静态类型注释检查代码是否存在错误。 这些类型容许您告诉Flow您但愿代码如何工做,Flow将确保它以这种方式工做。github
咱们知道react
源码如今仍是采用flow + js
的方式,下图截取一小段react Fiber源码
,先混个脸熟web
/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow */
import type {ReactElement} from 'shared/ReactElementType';
import type {ReactFragment, ReactPortal, ReactScope} from 'shared/ReactTypes';
import type {Fiber} from './ReactInternalTypes';
import type {RootTag} from './ReactRootTags';
import type {WorkTag} from './ReactWorkTags';
import type {TypeOfMode} from './ReactTypeOfMode';
import type {Lanes} from './ReactFiberLane.new';
import type {SuspenseInstance} from './ReactFiberHostConfig';
import type {OffscreenProps} from './ReactFiberOffscreenComponent';
复制代码
// @flow
function square(n: number): number {
return n * n;
}
square("2"); // Error!
复制代码
报错信息:正则表达式
Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ common/globFile.js:26:8npm
Cannot call square with '2' bound to n because string [1] is incompatible with number [2].编程
由于Flow很好地理解JavaScript,因此它不须要不少这些类型。 你应该只须要作不多的工做来描述你的Flow代码,它将推断其他部分。 在不少时候,Flow能够彻底理解您的代码而不须要任何类型json
// @flow
function square(n) {
return n * n; // Error!
}
square("2");
复制代码
报错信息: Cannot perform arithmetic operation because string [1] is not a number.
官方推荐babel
或flow-remove-types
npm install --save-dev @babel/cli @babel/preset-flow
复制代码
项目增长babel.config.js
文件
module.exports = function() {
return {
presets: [
"@babel/preset-flow"
]
}
}
复制代码
package.json
中添加scripts
{
"devDependencies": {
"@babel/cli": "^7.4.4",
"@babel/preset-flow": "^7.0.0",
},
"scripts": {
"build": "babel src/ -d lib/",
"prepublish": "npm run build"
}
}
复制代码
npm install --save-dev flow-bin
复制代码
package.json
中添加scripts
{
"devDependencies": {
"flow-bin": "^0.99.0"
},
"scripts": {
"flow": "flow"
}
}
复制代码
生成flowconfig
配置文件
npm run flow init
复制代码
运行flow
npm run flow
复制代码
生成相似INI格式,项目.flowconfig配置文件
3.1.1 .flowconfig
由6个部分组成
; 忽略匹配文件
[ignore]
<PROJECT_ROOT>/__tests__/.*
<PROJECT_ROOT>/lib/.*
; 包含指定的文件或目录
[include]
<PROJECT_ROOT>/src/.*
; 在类型检查代码时包含指定的库定义
[libs]
; lint
[lints]
all=warn
untyped-type-import=error
sketchy-null-bool=off
; 选项
[options]
all=true
esproposal.decorators=ignore
experimental.const_params=true
module.file_ext=.bar
module.use_strict=true
; 严格
[strict]
nonstrict-import
unclear-type
unsafe-getters-setters
untyped-import
untyped-type-import
; none
; 在声明模式下,代码没有进行类型检查,会检查文件内容
[declarations]
<PROJECT_ROOT>/third_party/.*
; 不检查文件内容,不匹配指定正则表达式的类型文件,丢弃类型并将模块视为任何模块
[untyped]
<PROJECT_ROOT>/third_party/.*
; 指定flow使用的版本
[version]
0.98.1
复制代码
3.1.2 # or ; or 💩 are ignored
# This is a comment
# This is a comment
; This is a comment
; This is a comment
💩 This is a comment
💩 This is a comment
复制代码
3.1.3 .flowconfig放置位置
.flowconfig的位置很是重要。Flow将包含.flowconfig的目录视为项目根目录。 默认状况下,Flow包含项目根目录下的全部源代码
vscode
推荐安装Flow Language Support
flow status // 启动flow后台进程
flow stop // 终止flow后台进程
复制代码
webpack
热加载,使用flow-webpack-plugin
'use strict';
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const FlowWebpackPlugin = require('flow-webpack-plugin');
module.exports = {
mode: 'development',
devtool: 'source-map',
entry: './example/app.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, './dist'),
},
devServer: {
hot: true,
disableHostCheck: true,
historyApiFallback: true
},
plugins: [
new HtmlWebpackPlugin({ template: 'example/index.html' }),
new FlowWebpackPlugin({
flowArgs: ['check']
})
],
};
复制代码
Flow后台进程使用此标志收集全部文件,并使用全部这些文件中提供的类型信息来确保一致性和无错误编程
使用JavaScript注释的形式,注释@flow
// @flow
或
/* @flow */
复制代码
忽略//@flow
,检查全部文件
flow check --all
复制代码
Flow后台进程将会捕获此错误
// @flow
function foo(x: ?number): string {
if (x) {
return x;
}
return "default string";
}
复制代码
# equivalent to `flow status`
flow
复制代码
运行flow检查
// @flow
function foo(x: ?number): string {
if (x) {
return x; // Cannot return `x` because number [1] is incompatible with string [2].
}
return "default string";
}
复制代码
类型注释符号
| // 或
& // 且
? // 可选
复制代码
类型注释中包括的类型
boolean // true or new Boolean(false)
string // "hello" or new String("world")
number // 3.14 or new Number(42)
null // null
undefined (void in Flow types) // undefined
Array (其中T用来描述数组中值的类型) // Array<T>
Object // {}
Function // function
class // class
Symbols (not yet supported in Flow) // Symbol("foo")
复制代码
小写
// @flow
function method(x: number, y: string, z: boolean) {
// ...
}
method(3.14, "hello", true);
复制代码
大写
// @flow
function method(x: Number, y: String, z: Boolean) {
// ...
}
method(new Number(42), new String("world"), new Boolean(false));
复制代码
boolean
// @flow
function acceptsBoolean(value: boolean) {
// ...
}
acceptsBoolean(true); // Works!
acceptsBoolean(false); // Works!
acceptsBoolean("foo"); // Error!
复制代码
JavaScript能够隐式地将其余类型的值转换为布尔值
if (42) {} // 42 => true
if ("") {} // "" => false
复制代码
非布尔值须要显式转换为布尔类型
// @flow
function acceptsBoolean(value: boolean) {
// ...
}
acceptsBoolean(0); // Error!
acceptsBoolean(Boolean(0)); // Works!
acceptsBoolean(!!0); // Works!
复制代码
string
// @flow
function acceptsString(value: string) {
// ...
}
acceptsString("foo"); // Works!
acceptsString(false); // Error!
复制代码
JavaScript能够隐式地将其余类型的值转换为字符
"foo" + 42; // "foo42"
"foo" + {}; // "foo[object Object]"
复制代码
Flow链接到字符串时只接受字符串和数字。
// @flow
"foo" + "foo"; // Works!
"foo" + 42; // Works!
"foo" + {}; // Error!
"foo" + []; // Error!
复制代码
必须明确并将其余类型转换为字符串
// @flow
"foo" + String({}); // Works!
"foo" + [].toString(); // Works!
"" + JSON.stringify({}) // Works!
复制代码
number
// @flow
function acceptsNumber(value: number) {
// ...
}
acceptsNumber(42); // Works!
acceptsNumber(3.14); // Works!
acceptsNumber(NaN); // Works!
acceptsNumber(Infinity); // Works!
acceptsNumber("foo"); // Error!
复制代码
null and void
// @flow
function acceptsNull(value: null) {
/* ... */
}
function acceptsUndefined(value: void) {
/* ... */
}
acceptsNull(null); // Works!
acceptsNull(undefined); // Error!
acceptsUndefined(null); // Error!
acceptsUndefined(undefined); // Works!
复制代码
Array
let arr: Array<number> = [1, 2, 3];
let arr1: Array<boolean> = [true, false, true];
let arr2: Array<string> = ["A", "B", "C"];
let arr3: Array<mixed> = [1, true, "three"]
复制代码
Object
// @flow
var obj1: { foo: boolean } = { foo: true };
var obj2: {
foo: number,
bar: boolean,
baz: string,
} = {
foo: 1,
bar: true,
baz: 'three',
};
复制代码
Function
// @flow
function concat(a: string, b: string): string {
return a + b;
}
concat("foo", "bar"); // Works!
// $ExpectError
concat(true, false); // Error!
复制代码
箭头Function
let method = (str, bool, ...nums) => {
// ...
};
let method = (str: string, bool?: boolean, ...nums: Array<number>): void => {
// ...
};
复制代码
回调Function
function method(callback: (error: Error | null, value: string | null) => void) {
// ...
}
复制代码
class
// @flow
class MyClass<A, B, C> {
constructor(arg1: A, arg2: B, arg3: C) {
// ...
}
}
var val: MyClass<number, boolean, string> = new MyClass(1, true, 'three');
class Foo {
serialize() { return '[Foo]'; }
}
class Bar {
serialize() { return '[Bar]'; }
}
// $ExpectError
const foo: Foo = new Bar(); // Error!
复制代码
Maybe Types
// @flow
function acceptsMaybeString(value: ?string) {
// ...
}
acceptsMaybeString("bar"); // Works!
acceptsMaybeString(undefined); // Works!
acceptsMaybeString(null); // Works!
acceptsMaybeString(); // Works!
acceptsMaybeString(12345); // Error!
// value: string null or undefined
复制代码
对象属性可选
// @flow
function acceptsObject(value: { foo?: string }) {
// ...
}
acceptsObject({ foo: "bar" }); // Works!
acceptsObject({ foo: undefined }); // Works!
acceptsObject({ foo: null }); // Error!问号放在string前不报错
acceptsObject({}); // Works!
复制代码
函数参数可选
// @flow
function acceptsOptionalString(value?: string) {
// ...
}
acceptsOptionalString("bar"); // Works!
acceptsOptionalString(undefined); // Works!
acceptsOptionalString(null); // Error!问号放在string前不报错
acceptsOptionalString(); // Works!
复制代码
带默认值的函数参数
// @flow
function acceptsOptionalString(value: string = "foo") {
// ...
}
acceptsOptionalString("bar"); // Works!
acceptsOptionalString(undefined); // Works!
acceptsOptionalString(null); // Error!
acceptsOptionalString(); // Works!
复制代码
使用字面文字做为类型
// @flow
function acceptsTwo(value: 2) {
// ...
}
acceptsTwo(2); // Works!
// $ExpectError
acceptsTwo(3); // Error!
// $ExpectError
acceptsTwo("2"); // Error!
复制代码
Union 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!
复制代码
Mixed Types
function stringifyBasicValue(value: string | number) {
return '' + value;
}
复制代码
A type based on another type
function identity<T>(value: T): T {
return value;
}
复制代码
An arbitrary type that could be anything
function getTypeOf(value: mixed): string {
return typeof value;
}
复制代码
Any Types
// @flow
function add(one: any, two: any): number {
return one + two;
}
add(1, 2); // Works.
add("1", "2"); // Works.
add({}, []); // Works.
复制代码
变量类型 将类型添加到变量声明 const let var
// @flow
const foo /* : number */ = 1;
const bar: number = 2;
var fooVar /* : number */ = 1;
let fooLet /* : number */ = 1;
var barVar: number = 2;
let barLet: number = 2;
复制代码
let
let foo: number = 1;
foo = 2; // Works!
// $ExpectError
foo = "3"; // Error!
复制代码
从新分配变量
let foo = 42;
if (Math.random()) foo = true;
if (Math.random()) foo = "hello";
let isOneOf: number | boolean | string = foo; // Works!
复制代码
从新分配变量肯定变量类型
// @flow
let foo = 42;
let isNumber: number = foo; // Works!
foo = true;
let isBoolean: boolean = foo; // Works!
foo = "hello";
let isString: string = foo; // Works!
复制代码
react
import * as React from 'react';
type Props = {
foo: number,
bar?: string,
};
type State = {
count: number,
};
class MyComponent extends React.Component<Props> {
state = {
count: 0,
};
render() {
this.props.doesNotExist; // Error! You did not define a `doesNotExist` prop.
return <div>{this.props.bar}</div>;
}
}
<MyComponent foo={42} />;
复制代码
想了解更多用法,可移步flow官方文档
:flow.org
此篇笔记已收录 我的笔记,感谢阅读,欢迎star
鼓励