@babel/plugin-transform-flow-strip-types
这个插件包含在 @babel/preset-flow 中
示例
🌐 Example
在
function foo(one: any, two: number, three?): string {}
出
function foo(one, two, three) {}
安装
🌐 Installation
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev @babel/plugin-transform-flow-strip-types
yarn add --dev @babel/plugin-transform-flow-strip-types
pnpm add --save-dev @babel/plugin-transform-flow-strip-types
bun add --dev @babel/plugin-transform-flow-strip-types
用法
🌐 Usage
使用配置文件(推荐)
🌐 With a configuration file (Recommended)
{
"plugins": ["@babel/plugin-transform-flow-strip-types"]
}
通过 CLI
🌐 Via CLI
babel --plugins @babel/plugin-transform-flow-strip-types script.js
通过 Node API
🌐 Via Node API
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:
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
这将在 Babel 8 中默认启用
启用时,只有当类型专用的类字段带有 declare 修饰符前缀时,它们才会被移除:
🌐 When enabled, type-only class fields are only removed if they are prefixed with the declare modifier:
class A {
declare foo: string; // Removed
bar: string; // Initialized to undefined
}
你可以在这里了解更多关于配置插件选项的内容