babel6 babel7_当您已经准备好Babel时设置Flow
babel6 babel7
by Jamie Kyle
杰米·凯尔(Jamie Kyle)
当您已经准备好Babel时设置Flow (Setting up Flow when you’ve already got Babel in place)
Flow is a static type checker for JavaScript. It makes you more productive by providing feedback as you write code. Flow gives you warnings in real-time that point out when you’ve made a mistake. If you would like to know more, check out flowtype.org.
Flow是JavaScript的静态类型检查器。 通过在编写代码时提供反馈,使您的工作效率更高。 Flow会实时为您提供警告,指出您犯错的时间。 如果您想了解更多信息,请查看flowtype.org 。
Rather than trying to build it’s own entirely separate ecosystem, Flow hooks into the existing JavaScript ecosystem. Using Babel to compile your code is one of the easiest ways to integrate Flow into a project.
Flow并没有尝试构建它自己的完全独立的生态系统,而是将其钩接到现有JavaScript生态系统中。 使用Babel编译代码是将Flow集成到项目中的最简单方法之一。
After two years of hard work, Babel works pretty much everywhere, just take a look at the setup page with integrations for every build tool you can imagine.
经过两年的努力,Babel几乎可以在任何地方工作,只需看一下设置页面,其中包含您可以想象的每个构建工具的集成。
If you don’t have Babel set up yet, you can use that guide to get set up. You also might want to checkout my handbook on Babel.
如果尚未设置Babel,则可以使用该指南进行设置。 您可能还想查看我关于Babel的手册 。
在Babel上设置Flow (Setting up Flow on top of Babel)
Once you have Babel set up, it’s easy to get going with Flow. First lets install two dependencies:
一旦设置好Babel,就可以轻松使用Flow。 首先让我们安装两个依赖项:
$ npm install --save-dev babel-plugin-transform-flow-strip-types$ npm install --global flow-bin
The Babel plugin is there in order to strip Flow types away so that your program runs. flow-bin is how you use Flow from the command line.
Babel插件在那里是为了删除Flow类型,以便您的程序运行。 flow-bin是从命令行使用Flow的方式。
Next, let’s add the Babel plugin to your .babelrc (or where-ever you are configuring Babel with options):
接下来,让我们将Babel插件添加到您的.babelrc中 (或使用选项配置Babel的任何地方):
{ presets: [...], plugins: [..., "transform-flow-strip-types"]}
Note: I’m assuming you are using Babel 6 for this tutorial. It is recommended that you upgrade if you have not already.
注意:我假设您在本教程中使用Babel 6。 如果尚未升级,建议您进行升级 。
Finally we’ll run flow init in our directory, which will create a .flowconfig file that should look like this:
最后,我们将在目录中运行flow init ,这将创建一个.flowconfig文件,该文件应如下所示:
[ignore]
[include]
[libs]
[options]
Awesome! Now we have Flow all set up in your project. How about we start making it run on some files?
太棒了! 现在,我们在您的项目中都设置了Flow。 我们如何开始使其在某些文件上运行呢?
使Flow运行 (Getting Flow running)
Flow is designed to be introduced to your repo incrementally. That means that you don’t have to add it to your whole codebase all at once. Instead, you can add it file-by-file as you go. Let’s start with something simple to get you going.
Flow旨在逐步引入您的仓库。 这意味着您不必一次将其添加到整个代码库中。 相反,您可以在运行时逐个文件添加它。 让我们从简单的事情入手。
First, try to find a file that doesn’t have a lot of complexity or external dependencies. Something with just a handful of plain functions to get started with.
首先,尝试查找没有太多复杂性或外部依赖性的文件。 只需少量的简单功能即可入门。
import {getNumberFromString} from "string-math-lib";
export function multiplyStrings(a, b) { return getNumberFromString(a) * getNumberFromString(b);}
In order to get Flow running on this file, we need to add a “flow pragma” comment at the top like this:
为了使Flow在此文件上运行,我们需要在顶部添加“ flow pragma”注释,如下所示:
// @flow
import {getNumberFromString} from "string-math-lib";
export function multiplyStrings(a, b) { return getNumberFromString(a) * getNumberFromString(b);}
This little comment at the top of the file tells Flow “Okay, I want you to type-check this file”.
文件顶部的小注释告诉Flow:“好吧,我要您对该文件进行类型检查”。
Now we need to actually run Flow for the first time. In order to do that, you’ll need to switch back to your terminal and run the following command:
现在我们需要第一次实际运行Flow。 为此,您需要切换回终端并运行以下命令:
$ flow
Note: This command is an alias of flow status.
注意:此命令是流状态的别名。
What this command does is start up a Flow server and asks it for the “status” of your repo, which will most likely return some errors for you to fix.
该命令的作用是启动Flow服务器,并要求其提供存储库的“状态”,这很可能会返回一些错误供您修复。
The most common errors that you’re gonna see in a new file are:
您将在新文件中看到的最常见错误是:
- “Missing annotation”“缺少注释”
- “Required module not found”“找不到所需的模块”
These errors are related to imports and exports. The reason they come up is a result of how Flow works. In order to check types across files, Flow looks directly at the imports and exports of each file.
这些错误与进口和出口有关。 它们出现的原因是Flow如何工作的结果。 为了检查跨文件的类型,Flow直接查看每个文件的导入和导出。
“缺少注释” (“Missing annotation”)
You’ll see an error like this from Flow because it relates somehow to an export of your file. Other than that Flow won’t complain about missing type annotations.
您会在Flow中看到这样的错误,因为它某种程度上与文件的导出有关。 除此之外,Flow不会抱怨缺少类型注释。
So in order to fix that, we can start adding some types to your file. For a detailed guide on how to do that see the user guide. What you should end up is with some types like this:
因此,为了解决此问题,我们可以开始向您的文件添加一些类型。 有关如何执行此操作的详细指南, 请参阅用户指南 。 您最终应该使用的是以下某些类型:
import {getNumberFromString} from "string-math-lib";
export function multiplyStrings(a: string, b: string): number { return getNumberFromString(a) * getNumberFromString(b);}
Keep running flow as you add your types to see the effects of what you are doing. Eventually you should be able to clear out all the “Missing annotation” errors.
添加类型时,请保持运行顺畅 ,以查看所做操作的效果。 最终,您应该能够清除所有“缺少注释”错误。
“找不到所需的模块” (“Required module not found”)
You’ll get these errors whenever you an import/require that can’t be resolved using Node’s normal module algorithm or if you have ignored it with your .flowconfig.
只要导入/请求无法使用Node的常规模块算法解决,或者使用.flowconfig忽略了导入/请求,就会出现这些错误。
This can be caused by a lot of things, maybe you’re using a special webpack resolver, maybe you forgot to install something. Whatever the reason, Flow needs to be able to find the module you are importing to do its job correctly. You have a couple options on how to solve this:
这可能是由很多原因引起的,也许您正在使用特殊的Webpack解析器,或者您忘记安装某些东西。 无论出于何种原因,Flow都必须能够找到您要导入的模块才能正确执行其工作。 您有几种解决方法:
module.name_mapper — specify a regular expression to match against module names, and a replacement pattern.
module.name_mapper —指定一个正则表达式以与模块名称匹配,以及一个替换模式。
- Create a library definition for the missing module为缺少的模块创建库定义
We’ll focus on creating a library definition for the module, if you need to use module.name_mapper you can see more about it in the documentation.
我们将专注于为模块创建库定义,如果您需要使用module.name_mapper ,则可以在文档中找到有关它的更多信息。
创建库定义 (Creating a library definition)
Having library definitions is useful for giving types to the packages you have installed that don’t have types. Let’s set one up for our string-math-lib library from the previous example.
具有库定义对于为已安装的没有类型的软件包提供类型很有用。 让我们为上一个示例中的string-math-lib库设置一个。
First create a flow-typed directory in your root directory (the directory where you put your .flowconfig).
首先,在根目录(放置.flowconfig的目录)中创建一个流类型的目录。
You can use other directory names using the [lib] section of your .flowconfig. However, using flow-typed will work out of the box.
您可以使用.flowconfig的[lib]部分使用其他目录名称。 但是,使用流类型将立即可用。
Now we’ll create a flow-typed/string-math-lib.js file to hold our “libdef” and start it off like this:
现在,我们将创建一个流类型的/string-math-lib.js文件来保存我们的“ libdef”并像这样启动它:
declare module "string-math-lib" { // ...}
Next we just need to write definitions for exports of that module.
接下来,我们只需要为该模块的导出编写定义。
declare module "string-math-lib" { declare function getNumberFromString(str: string): number}
Note: If you need to document the “default” or primary export, you can do that with declare module.exports: or declare export default
注意:如果需要记录“默认”或主导出 ,则可以使用clarify module.exports:或声明默认导出
There’s a lot more to library definitions so you should read through the documentation and read this blog post on how to create high-quality libdefs.
库定义还有很多,因此您应该通读文档,并阅读有关如何创建高质量libdefs的博客文章 。
从流类型安装libdef (Installing a libdef from flow-typed)
Because libdefs can consume a lot of time, we maintain an official repository of high-quality libdefs for all sorts of packages called flow-typed.
因为libdefs会花费很多时间,所以我们维护一个高质量libdefs的官方存储库,用于存放称为flow-typed的各种软件包。
To get started with flow-typed, install the command line interface (CLI) globally:
要开始使用流类型,请全局安装命令行界面(CLI):
$ npm install --global flow-typed
Now you can look within flow-typed/definitions/npm to see if there’s an existing libdef for a package you want to use, if there is you can install it like this:
现在您可以在流类型/定义/ npm中查看 查看是否有要使用的软件包的现有libdef,如果有,可以这样安装:
$ flow-typed install chalk@1.0.0 --flowVersion 0.30
This tells flow-typed that you want to install the chalk package at the 1.0.0 version when you are running Flow 0.30.
这告诉Flow-typed您要在运行Flow 0.30时安装1.0.0版本的粉笔包。
The flow-typed CLI is still in beta and there is a lot of improvements planned for it, so expect this to improve a lot in the near future.
流类型的 CLI仍处于beta中,并且计划对其进行很多改进,因此希望在不久的将来会有所改进。
Be sure to contribute back to the flow-typed libdefs. It’s a community effort, and the more people that contribute, the better it gets.
确保对流类型的libdefs有所贡献。 这是社区的努力,贡献的人越多,收益越好。
您可能会遇到的其他错误: (Other errors you might run into:)
Hopefully, we’ve covered just about everything that you will run into while getting started with Flow. However, you also might run into some errors like this:
希望我们已经介绍了Flow入门时您将遇到的所有内容。 但是,您也可能会遇到类似以下的错误:
Package inside of node_modules is reporting errors
node_modules内部的软件包报告错误
Reading node_modules is taking a really long time
读取node_modules花费很长时间
Malformed JSON inside node_modules is causing errors
node_modules内部的JSON格式错误导致错误
There’s a handful of reasons for these types of errors that I won’t get into in this post (I’m working on detailed documentation for each individual error). For now, in order to keep yourself moving, you can just [ignore] the files that are causing these errors.
这些错误类型有很多原因,我在本文中不会涉及(我正在为每个错误编写详细的文档)。 现在,为了让自己动起来,您可以[忽略]导致这些错误的文件。
This means adding file paths to your [ignore] section in your .flowconfig like this:
这意味着将文件路径添加到.flowconfig中的 [ignore]部分,如下所示:
[ignore]./node_modules/package-name/*./node_modules/other-package/tests/*.json
[include]
[libs]
[options]
There is generally better options than this, but this should give you a good place to start.
通常有比这更好的选择,但这应该为您提供一个良好的起点。
For some examples of how to better handle errors within node_modules see this Stack Overflow answer about fbjs.
有关如何更好地处理node_modules中的错误的一些示例,请参见有关fbjs的Stack Overflow答案 。
专家提示:查看您的健康状况如何 (Pro tip: Checking to see how well you’re covered)
If you’re ever wondering how well a file is covered by Flow, you can use the following command to see a coverage report:
如果您想了解Flow对文件的覆盖程度,可以使用以下命令查看覆盖率报告:
$ flow coverage path/to/file.js --color
其他资源和支持 (Additional Resources and Support)
There’s lots that was not covered by this article. So here are some links to resources that can help you out.
本文没有涉及很多内容。 因此,这里有一些可以帮助您的资源链接。
Flow Website
流网站
Try Flow Online
在线试用Flow
Flow GitHub
流GitHub
Stack Overflow #flowtyped
堆栈溢出#flowtyped
The Flow team is committed to making sure that everyone has an excellent experience using Flow. If that is ever not true, we’d love to hear from you about how to improve.
Flow团队致力于确保每个人在使用Flow方面都有出色的经验。 如果那不是真的,我们很乐意听取您关于如何改进的信息。
Follow James Kyle on twitter. Follow Flow on twitter too.
在Twitter上关注James Kyle 。 在Twitter上也关注Flow 。
翻译自: https://www.freecodecamp.org/news/using-flow-with-babel-c04fdca8d14d/
babel6 babel7
相关文章:

如何为Android上的产品设计一款合适的图标
如 果你已经完成了你的app,你一定会马上向其它人宣布这件事情。但是你需要注意一个很重要的问题,那就是app的图标。你的图标可能在项目启动之 前就已经设计好了,但我不喜欢这样,如果app没有完成实际上图标也没什么用了。如果你不是…

得到windows聚焦图片(windows 10)
有些Windows聚焦图片确实很漂亮,很希望保留下来,但是Windows聚焦图片总更好,网上有得到聚焦图片的方法,每次都手动去弄真麻烦,于是自己编了一个小程序,自动得到Windows聚焦图片,下面是运行这个小…

swift 加载gif 框架图片
swift 加载gif 框架图片 SwiftGifOrigin 以下代码 轻松搞定 let imgView UIImageView(frame: CGRect(x: 50, y: 100, width: 280, height: 200));imgView.loadGif(name: "gfff");self.view.addSubview(imgView);

devops_最低可行DevOps
devopsby Michael Shilman通过迈克尔希尔曼(Michael Shilman) 最低可行DevOps (Minimum Viable DevOps) 快速而肮脏的指南,用于扩展您的发布并拥抱互联网的死亡 (A quick and dirty guide to scaling your launch and embracing the Internet hug of death) Startu…

java基础之——类的初始化顺序(转载)
原文地址:http://www.cnblogs.com/chrischennx/p/3612295.html 由浅入深,首先,我们来看一下,一个类初始化有关的都有些啥米: 静态成员变量、静态代码块、普通成员变量、普通代码块、构造器。(成员方法&…

如何用CSS快速布局(一)—— 布局元素详细
要快速进行网页排版布局,则必须对布局的元素有清晰的了解,才不会总是在细节处出错。这一篇先详解有关布局的因素作为布局基础:块级元素and内联元素、盒模型、准确定位、元素对齐、样式继承。下一篇则重点描述快速布局思路。 一、什么是块级元…

iOS 改变字符串中数字的颜色
匹配中文字符 [\u4e00-\u9fa5] 匹配双字节字符(包括汉字在内) [^\x00-\xff] 匹配网址:[a-zA-z]://[^\s]* 匹配国内电话 \d{3}-\d{8}|\d{4}-\{7,8} 匹配腾讯QQ号 [1-9][0-9]{4,} 匹配18位身份证号^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$ Swift String…

jpg在线合并jpg_JPG如何运作
jpg在线合并jpgby Colt McAnlis通过Colt McAnlis JPG如何运作 (How JPG Works) The JPG file format was one of the most technologically impressive advancements to image compression to come on the scene in 1992. Since then, it’s been a dominant force in represe…

革命就是请客吃饭(案例分析吧)
前不久路过南京, 拜会了本科同学. 刚好他要见个青年才俊谈些事情, 于是就神神秘秘地把我拉了过去. 一路高谈阔论, 指点江山, 忆往昔峥嵘岁月之后, 此次"拜访"的目的也渐渐清晰起来. 我们所要见的人是位年轻的创业者, 他有些移动互联网的点子, 想和我们分享下, 并尝试…

TextView-- 测量文字宽度
https://my.oschina.net/lengwei/blog/637380; http://blog.csdn.net/mare_blue/article/details/51388403; http://blog.csdn.net/baidu_31093133/article/details/52413893; --1,Android中调用Paint的measureText()方法取得字符串显示的宽度值: public static float GetTextW…

swift 简单风格的Toaster
简单风格的Toaster Toaster //1 弹出文本 "Hello World" 延迟时间 2 展示时间 1 Toast(text: "Hello World", delay: 2, duration: 1).show();//2 初始化toast 方法 let toast Toast(text: "你好世界"); toast.show(); toast.cancel();// 3 …

工业革命前数千年人口经济_我们已经进行了数千年的编程
工业革命前数千年人口经济by Tautvilas Mečinskas由TautvilasMečinskas 我们已经进行了数千年的编程 (We have been programming for thousands of years) Computer programs are all around us. We interact with them every day. It looks as if software is becoming mor…

1-flutter 安装步骤
flutter 安装步骤 1 下载SDK SDK 下载地址 2 解压压缩包 将sdk 文件夹丢进系统的应用程序(Application)的目录 3 配置环境变量 命令行 open ~/.bash_profile ,然后在bash 文件中写入下面配置 export PATH$PATH:/Applications/flutter/bi…

codevs1258 关路灯(☆区间dp)
1258 关路灯 时间限制: 1 s空间限制: 128000 KB题目等级 : 大师 Master题目描述 Description多瑞卡得到了一份有趣而高薪的工作。每天早晨他必须关掉他所在村庄的街灯。所有的街灯都被设置在一条直路的同一侧。 多瑞卡每晚到早晨5点钟都在晚会上,然后他开始关灯。开…

BroadcastReceiver自学笔记
1. 使用步骤: 1.1 声明Intent Intent intent new Intent("name");------静态常用 IntentFilter filter new IntentFilter("name");--------动态常用 1.2 注册 1.3 接收:利用action或者Bundle 在OnReceive()中,接收信息…

小狗钱钱_小狗设计
小狗钱钱by Patryk Adaś通过PatrykAdaś 小狗设计 (Design for doggies) Education should be universal and free for everyone.教育应该是普及的,对所有人来说都是免费的。 When I first entered the field, designers were patient with me. They gave me fee…

层化(stratification)的方法
有时候我们会遇到调整后的模型反而不如调整前表现好的情况,这可能和数据的随机分割有关系。在这个不平衡的数据情况下,最好用层化(stratification)的方法,比如: from sklearn.cross_validation import Stra…

零基础入门jQuery视频教程
零基础入门jQuery最新版开发.NET富客户端应用(选择器、DOM操作、事件和动画、Ajax应用、插件、Mobile)课程分类:.NETJquery适合人群:初级课时数量:35课时用到技术:javascript,ajax,jquery,handler涉及项目:各知识点的项…

2-flutter 之HelloWorld
widget 在flutter 中,几乎所有的东西都是widget,本身是用户界面的基本构建快,将widget组成一个层次结构, 调用widget树。每一个窗口widget都嵌套在父窗口widget中,并且从其父窗口中继承属性。甚至应用程序对象本身也…

浏览器获取浏览历史_浏览器历史的未来
浏览器获取浏览历史by Patryk Adaś通过PatrykAdaś 浏览器历史的未来 (The Future of Browser History) I am really unsatisfied with the current state of Browser History. I think that this is the most underestimated feature of every modern web browser. Let’s t…

【水】JSOI完美的对称
完美的对称 题目描述 在峰会期间,必须使用许多保镖保卫北约组织的各国代表。代表们除了由他自己的随身保镖保护外,组委会还指派了一些其他的特工和阻击手保护他们。为了使他们的工作卓有成效,使被保卫的人的安全尽可能得到保障,保…

3-flutter 项目结构 资源 依赖
1 项目的名称 android 安卓相关工程文件 build 项目的构建输出目录 ios ios 相关的部分工程文件 lib 项目中的dart 源文件 src 包含其他的源文件main.dart 自动生成的项目入口文件 test 测试相关的文件 pubspec.ymal 项目依赖配置文件 3 归档图片资源和处理不同的分辨率…

python简史_命令行简史
python简史by Gitter通过吉特 命令行简史 (A Brief History of the Command Line) This post by Andy Trevorah, Engineer at Gitter, has been adapted from a talk that he originally gave at codebar, a non-profit initiative that facilitates the growth of a diverse …

4- flutter - Widget
Widget Flutter 中的view 就是widget 1 无状态和有状态的Widget StateslessWidgets 适用于用户界面不依赖于用户的信息的时候 StatesfulWidgets 有状态的,例如HTTP 网络请求或者用户交互之后收到数据动态表更新UI 这就是一个无状态的Widget Text("we like…

第一讲SQL命令的DDL和DML操作讲解
知识点: 一、sql命令DDL(结构化操作) 二、sql命令DML操作(增删改查) 1.sql命令DDL(结构化操作) 1.1表添加字段: alter table 表名 add 列定义 如: alter table Student add email varchar(128); 1.2 修改字段ÿ…
基于Tkinter利用python实现颜色空间转换程序
主要基于colorsys实现,例子是从hls转换到rgb,假设要换颜色空间非常easy仅仅须要改动一个函数 用到了Scale和Canvas组件 代码例如以下: from Tkinter import * import colorsys #操作后的响应函数 def update(* args):colorr,g,b colorsys.hl…

react 时刻表插件_React“啊哈”的时刻
react 时刻表插件As a teacher, one of my main goals is to maximize people’s “aha” moments.作为一名老师,我的主要目标之一是最大限度地利用人们的“哈哈”时刻。 An “aha” moment is a moment of sudden insight or clarity, where the subject starts t…

同样在JavaScript中
ES6有三个内置决定一些设施x和一些y是“相同的”。 它们是:平等或“双等于”(),严格平等或平等“三重”(),Object.is。 (注意,Object.is在ES6补充道。 等于两倍和三倍等于存在ES6之前,和他们的行为没有改变。) 概述 演示,下面是三个同样使用的比较: x y x y Object。是(x,…

5-flutter 布局和列表
布局和列表 类型作用特点Container只有一个子 Widget。默认充满,包含了padding、margin、color、宽高、decoration 等配置。Padding只有一个子 Widget。只用于设置Padding,常用于嵌套child,给child设置padding。Center只有一个子 Widget。只…

shell awk实战
一、文本处理 1、按行提取关键字频次(如取第5列) awk BEGIN{FS"|"} {a[$5]1;} END {for(i in a) print i ":" a[i];} OPT.ForumLogicNewServer_action_20161107.log | sort -nrk 2 -t : 2、日志用户每分钟访问量统计 这里我们统计日…