LoginSignup
5
4

More than 5 years have passed since last update.

BabelなしでFlowを使う

Posted at

基本的にFlowを使うときは、Babelを用いてコンパイルを前提していると思うが、簡易的なスクリプトを書くときでもFlowは有効だ。

Flowには、Comment Typesと呼ばれる機能があり、コメントベースのシンタックスが用意されている。

Comment Types | Flow

// @flow
function sum(a /*: number */ , b /*: number */) {
    return a + b
}

sum('hello', 2)

基本的には、/*: 型 */と書くだけ。
型チェックによって思わぬバグを防げるし、ESDocやJSDocなどでコメントをつけるより気軽である。

スクリーンショット 2017-06-05 16.13.02.png

また、エディタの支援により、補完されるのも嬉しい。

スクリーンショット 2017-06-05 16.12.47.png

このコメント形式のFlowを採用しているライブラリをいくつか挙げておく。

ast-pretty-print/index.js at master · babel-utils/ast-pretty-print

babel-flow-scope/index.js at master · babel-utils/babel-flow-scope

thejameskyle/read-file-async: Typed async readFile function

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4