@babel/preset-flow

This preset is recommended if you use Flow, a static type checker for JavaScript code. It includes the following plugins:

Example

In

JavaScript

  1. function foo(one: any, two: number, three?): string {}

Out

JavaScript

  1. function foo(one, two, three) {}

Installation

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/preset-flow
  1. yarn add --dev @babel/preset-flow
  1. pnpm add --save-dev @babel/preset-flow

Usage

babel.config.json

  1. {
  2. "presets": ["@babel/preset-flow"]
  3. }

Via CLI

Shell

  1. babel --presets @babel/preset-flow script.js

Via Node API

JavaScript

  1. require("@babel/core").transformSync("code", {
  2. presets: ["@babel/preset-flow"],
  3. });

Options

all

boolean, defaults to false.

Flow will only parse Flow-specific features if a @flow pragma is present atop the file, or the all option is set inside the .flowconfig.

If you are using the all option in your Flow config, be sure to set this option to true to get matching behavior.

For example, without either of the above set, the following call expression with a type argument:

  1. f<T>(e)

Would get parsed as a nested binary expression:

JavaScript

  1. f < T > e;

allowDeclareFields

boolean, defaults to false

Added in: v7.9.0

@babel/preset-flow - 图1note

This will be enabled by default in Babel 8

When enabled, type-only class fields are only removed if they are prefixed with the declare modifier:

JavaScript

  1. class A {
  2. declare foo: string; // Removed
  3. bar: string; // Initialized to undefined
  4. }

ignoreExtensions

boolean, defaults to true

Added in: v7.24.0

When it is set to true, Babel will apply the flow transform to all extensions. When it is set to false, Babel will avoid the flow transform for *.tsx files.

experimental_useHermesParser

boolean, defaults to false

Added in: v7.24.0

The Hermes team is maintaining an alternative Flow parser for Babel, which is better kept up-to-date with the latest Flow syntax features. You can enable it by setting this option to true.

@babel/preset-flow - 图2warning

The Hermes parser does not currently attach comments to the AST. This can cause problems with transforms that depend on the presence of specific comments.

You can read more about configuring preset options here