Skip to main content

@babel/plugin-transform-flow-strip-types

info

这个插件包含在 @babel/preset-flow 中

示例​

🌐 Example

在

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

出

JavaScript
function foo(one, two, three) {}

安装​

🌐 Installation

npm install --save-dev @babel/plugin-transform-flow-strip-types

用法​

🌐 Usage

🌐 With a configuration file (Recommended)

babel.config.json
{
"plugins": ["@babel/plugin-transform-flow-strip-types"]
}

通过 CLI​

🌐 Via CLI

Shell
babel --plugins @babel/plugin-transform-flow-strip-types script.js

通过 Node API​

🌐 Via Node API

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-flow-strip-types"],
});

选项​

🌐 Options

all​

boolean,默认值为 false。

只有在文件顶部存在 @flow 指令,或者在 .flowconfig 中设置了 all 选项 时,Flow 才会解析 Flow 特有的功能。

🌐 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.

如果你在 Flow 配置中使用 all 选项,记得把这个选项设置为 true,这样行为才能匹配。

🌐 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, the following call expression with a type argument:

f<T>(e)

将被解析为嵌套的二进制表达式:

🌐 Would get parsed as a nested binary expression:

JavaScript
f < T > e;

requireDirective​

boolean,默认值为 false。

把这个设置为 true 会只从包含 // @flow 指令的文件中去掉注释和声明。对于没有该指令的文件中出现的任何 Flow 注释,也会报错。

🌐 Setting this to true will only strip annotations and declarations from files that contain the // @flow directive. It will also throw errors for any Flow annotations found in files without the directive.

allowDeclareFields​

boolean,默认值为 false

已添加于:v7.9.0

🌐 Added in: v7.9.0

note

这将在 Babel 8 中默认启用

启用时,只有当类型专用的类字段带有 declare 修饰符前缀时,它们才会被移除:

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

JavaScript
class A {
declare foo: string; // Removed
bar: string; // Initialized to undefined
}
tip

你可以在这里了解更多关于配置插件选项的内容