javascript的扩展:如何使用flow静态类型进行检查报错

本篇文章给大家带来的内容是关于javascript的扩展:如何使用flow静态类型进行检查报错,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

js 语言与 java、C 系列等语言有一点很大的不同,就是 js 语言是弱类型语言。js 语言的这个特性可能让大家觉得 js 很自由,没有强制性的约束,但是当遇到大型项目的时候,js 的这个特性就会变得比较麻烦,因为这会导致团队的代码很不可控。这个原因也是促使 TypeScript 诞生的一个很重要的原因。

但其实很多开发人员还是比较喜欢用 js 来开发项目,所以 facebook 开发出 flow 来帮助 js 语言扩展静态类型检查功能,规避上面提到的问题。

1. 代码示例

flow 规定,在需要做 ‘flow 静态类型检查’ 文件的开头加上 // @flow 这段注释,让工具识别这个文件需要做静态类型检查,否则就会当作一般 js 文件对待,不做静态类型检查。

flow 静态类型几乎可以应用到所有的 js 对象,包括 es6 扩展的 class, module 等,也包括 jsx 语法。

立即学习“Java免费学习笔记(深入)”;

以下是一些基础的静态类型举例,更详细的可以查看 Type Annotations | Flow.

1.1 基本类型

与 js 的基本数据类型类似,包括:

boolean: 对应 js 的 Boolean 类型

number: 对应 js 的 Number 类型

string: 对应 js 的 String 类型

null: 对应 js 的 null

void: 对应 js 的 undefined

正常的 js 代码

let hello = 'hello'; // 声明一个变量hello = 2 * 2; // 重新赋值hello = []; // 重新赋值

登录后复制

加上 flow 静态类型检查扩展的代码

// @flowlet hello: string = 'hello'; // 声明一个 string 类型的变量hello = 2 * 2; // 报错hello = []; // 报错hello = 'hi'; // 重新赋值

登录后复制

1.2 函数

正常的 js 代码

function plus(a, b) {  return a + b;}plus(); // NaNplus(1); // NaNplus(1, 2); // 3plus('hello'); // 'helloundefined'plus('hello', ' hi'); // 'hello hi'plus({}, {}); // '[object Object][object Object]'

登录后复制

加上 flow 静态类型检查扩展的代码

// @flow// 定义一个 '两个数字参数,返回值也是数字' 的函数function plus(a: number, b: number): number {  return a + b;}plus(); // 报错plus(1); // 报错plus('hello'); // 报错plus('hello', ' hi'); // 报错plus({}, {}); // 报错plus(1, 2); // 3

登录后复制登录后复制登录后复制

1.3 可能(Maybe),可选(Optional),语义(Literal),混合(Mixed)

可能(Maybe)类型用一个 ? 在类型前面表示,包含类型本身、null、undefined

// @flowlet hello: ?string; // 声明一个数据类型可以是 string, null, undefined 的变量hello = null; // 赋值hello = undefined; // 重新赋值hello = 'hello'; // 重新赋值hello = 1; // 报错hello = true; // 报错

登录后复制

可选(Optional)类型一般用于对象属性或者函数参数,在名称后面加一个 ?,包含类型本身、undefined

// @flowconst obj: {hello? : string}; // 属性 hello 可以是 string, undefinedobj = {}; // 赋值obj = {hello: undefined}; // 重新赋值obj = {hello: 'hello'}; // 重新赋值obj = {hello: null}; // 报错obj = {hello: 1}; // 报错obj = {hello: true}; // 报错// 属性 param 可以是 number, undefinedfunction method(param?: number) { /* ... */ }method(); // 正常method(undefined); // 正常method(1.12); // 正常method(null); // 报错method('hello'); // 报错

登录后复制

语义(Literal)类型一般用于声明某个,某几个特定的值(多个值用 | 分隔)

// @flowlet hello: 'hello'; // 声明一个只能赋值 'hello' 的变量hello = 'hello'; // 赋值hello = 'hi'; // 报错hello = 12; // 报错hello = undefined; // 报错hello = null; // 报错function method(param: 1 | 'hi' | boolean): void { /* ... */ }method(); // 报错,缺少参数method(1); // okmethod(1.2); // 报错,类型不对method('hi'); // okmethod('hello'); // 报错,类型不对method(true); // okmethod(false); // ok

登录后复制

混合(Mixed)类型是指任意数据类型

// @flowlet hello: mixed; // 声明一个 mixed 类型的变量hello = 'hello'; // 赋值hello = 'hi'; // 重新赋值hello = 12; // 重新赋值hello = undefined; // 重新赋值hello = null; // 重新赋值

登录后复制

1.4 复合类型

数组

// @flowlet arr1: Array = [true, false, true]; // 声明一个元素是 boolean 的数组arr1 = [true, 1]; // 报错,1 不是 boolean 值arr1 = ['']; // 报错,'' 不是 boolean 值let arr2: Array = ["A", "B", "C"]; // 声明一个元素是 string 的数组let arr3: Array = [1, true, "three"] // 声明一个元素是任意类型的数组arr1 = [true, 1]; // 重新赋值 arr1 = ['']; // 重新赋值

登录后复制

map

// @flow// 声明一个 map 类型,其有一个名为 foo,类型 boolean 的子元素let obj1: { foo: boolean } = { foo: true };obj1 = {}; // 报错,缺少 foo 这个属性值obj1 = {foo: 1}; // 报错,属性值 foo 的类型必须是 booleanobj1 = {foo: false, bar: 'hello'}; // 重新赋值// 声明一个 map 类型,其有名为 foo, bar, baz,类型 number, boolean, string 的子元素let obj2: {  foo: number,  bar: boolean,  baz: string,} = {  foo: 1,  bar: true,  baz: 'three',};

登录后复制

更静态类型可以查看 Type Annotations | Flow.

2. 使用工具

安装

# 全局安装npm i -g flow-bin# 本地安装npm i -D flow-bin

登录后复制

使用

flow init                       # 初始化项目flow check path/to/dir          # 检查这个目录下所有的文件flow check path/to/js/file      # 检查指定文件

登录后复制

3. 配合 babel 一起使用

因为 flow 静态类型只是对 js 的扩展,并不是 js 原生支持的,也不能直接运行,所以,一般 flow 都是配合 babel 一起使用的,这样就可以在程序运行的时候进行静态类型检查,达到我们想要的效果。

3.1 babel-preset-flow

安装 babel-preset-flow,这样 babel 在转码 js 文件时就能识别 flow 的语法。

npm i -D babel-preset-flow

登录后复制

.babelrc

{  "presets": ["flow"]}

登录后复制

源文件(flow)

// @flow// 定义一个 '两个数字参数,返回值也是数字' 的函数function plus(a: number, b: number): number {  return a + b;}plus(); // 报错plus(1); // 报错plus('hello'); // 报错plus('hello', ' hi'); // 报错plus({}, {}); // 报错plus(1, 2); // 3

登录后复制登录后复制登录后复制

转码后的文件

// 定义一个 '两个数字参数,返回值也是数字' 的函数function plus(a, b) {  return a + b;}plus(); // 报错plus(1); // 报错plus('hello'); // 报错plus('hello', ' hi'); // 报错plus({}, {}); // 报错plus(1, 2); // 3

登录后复制

3.2 babel-plugin-flow-runtime

一般会在开发环境下,使用 babel-plugin-flow-runtime 插件,这样就可以在开发的时候,实时检查数据类型,就像原生的运行 flow 静态类型检查一样。(一般在产品环境不会使用这个功能,因为会额外消耗 js 的性能)

npm i -D babel-plugin-flow-runtime flow-runtime

登录后复制

.babelrc

{  "presets": ["flow"],  "plugins": ["flow-runtime"]}

登录后复制

源文件(flow)

// @flow// 定义一个 '两个数字参数,返回值也是数字' 的函数function plus(a: number, b: number): number {  return a + b;}plus(); // 报错plus(1); // 报错plus('hello'); // 报错plus('hello', ' hi'); // 报错plus({}, {}); // 报错plus(1, 2); // 3

登录后复制登录后复制登录后复制

转码后的文件

import t from 'flow-runtime';// 定义一个 '两个数字参数,返回值也是数字' 的函数function plus(a, b) {  return a + b;}t.annotate(plus, t.function(t.param('a', t.number()), t.param('b', t.number()), t.return(t.number())));plus(); // 报错plus(1); // 报错plus('hello'); // 报错plus('hello', ' hi'); // 报错plus({}, {}); // 报错plus(1, 2); // 3

登录后复制

这个时候,js 文件就会导入 flow-runtime 模块,对 plus 函数的参数 a, b 和返回值进行数据类型检查,如果不符合数据定义,就会报错。

以上就是javascript的扩展:如何使用flow静态类型进行检查报错的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2740461.html

(0)
上一篇 2025年3月8日 02:18:56
下一篇 2025年3月5日 19:03:04

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论