Flow - Facebook 家出品的 Javascript 静态类型检查工具

955 阅读1分钟
原文链接: flowtype.org

What is Flow?

Flow is a static type checker, designed to find type errors in JavaScript programs:

/* @flow */
function foo(x) {
  return x * 10;
}
foo('Hello, world!');
$> flow
hello.js:5:5,19: string
This type is incompatible with
  hello.js:3:10,15: number

Flow also lets you gradually evolve JavaScript code into typed code:

/* @flow */
function foo(x: string, y: number): string {
  return x.length * y;
}
foo('Hello', 42);
$> flow
hello.js:3:10,21: number
This type is incompatible with
  hello.js:2:37,42: string

Typed Flow code easily transforms down to regular JavaScript, so it runs anywhere.

Why Flow?

The goal of Flow is to find errors in JavaScript code with little programmer effort. Flow relies heavily on type inference to find type errors even when the program has not been annotated - it precisely tracks the types of variables as they flow through the program.

At the same time, Flow is a gradual type system. Any parts of your program that are dynamic in nature can easily bypass the type checker, so you can mix statically typed code with dynamic code.

Flow also supports a highly expressive type language. Flow types can express much more fine-grained distinctions than traditional type systems. For example, Flow helps you catch errors involving null, unlike most type systems.

We first introduced Flow at the @Scale Conference in September:

Using Flow

Start with our Getting Started guide to download and try Flow yourself. Flow is open-source, so you can also start with the code on the GitHub repo.

Flow is still evolving: it is already used within Facebook, and we want to continue to develop it in the open. We hope it will be useful for other JavaScript projects, so please try it out, join the community and give us feedback!