Strip flow type annotations from your output code.
npx @tessl/cli install tessl/npm-babel--plugin-transform-flow-strip-types@7.27.0@babel/plugin-transform-flow-strip-types is a Babel plugin that strips Flow type annotations from JavaScript and TypeScript code during compilation. It processes Flow directives, removes type-only imports and exports, and systematically eliminates all Flow type constructs while preserving the executable code.
npm install --save-dev @babel/plugin-transform-flow-strip-typesimport transformFlowStripTypes from "@babel/plugin-transform-flow-strip-types";For CommonJS:
const transformFlowStripTypes = require("@babel/plugin-transform-flow-strip-types");// Basic Babel configuration
{
"plugins": ["@babel/plugin-transform-flow-strip-types"]
}
// With options
{
"plugins": [
["@babel/plugin-transform-flow-strip-types", {
"requireDirective": false,
"allowDeclareFields": false
}]
]
}// Using programmatically with Babel
import { transform } from "@babel/core";
import transformFlowStripTypes from "@babel/plugin-transform-flow-strip-types";
const result = transform(code, {
plugins: [transformFlowStripTypes]
});The main export is a Babel plugin factory function that creates a plugin instance with visitor methods for stripping Flow types.
/**
* Creates a Babel plugin for stripping Flow type annotations
* @param api - Babel API object
* @param opts - Plugin configuration options
* @returns Babel plugin configuration object
*/
declare function transformFlowStripTypes(
api: any,
opts: Options
): {
name: string;
inherits: any;
visitor: BabelVisitor;
};
export default transformFlowStripTypes;Plugin configuration options for controlling stripping behavior.
/**
* Configuration options for the Flow strip types plugin
*/
interface Options {
/**
* When true, only strips Flow types if a @flow directive is present
* When false (default), strips Flow types regardless of directive
*/
requireDirective?: boolean;
/**
* When true, allows class properties with declare modifier
* When false (default), throws error for declare fields (Babel 7 only)
*/
allowDeclareFields?: boolean;
}The plugin systematically removes the following Flow-specific syntax:
let x: string = "hello"function foo(x: number): stringfunction foo(): stringclass C { prop: string }import type { User } from "./types"import typeof { api } from "./api"foo<T>()new Foo<T>()<Component<T> />foo?.<T>()class C implements Interfacedeclare prop: string(value: any)// @flow, // @flow strictfunction foo(x?: string)function isString(x): x %checksThe plugin throws compilation errors in specific scenarios:
/**
* Error thrown when requireDirective is true but no @flow directive found
*/
class FlowDirectiveRequiredError extends Error {
message: "A @flow directive is required when using Flow annotations with the `requireDirective` option.";
}
/**
* Error thrown when declare fields are used without allowDeclareFields option (Babel 7)
*/
class DeclareFieldsNotAllowedError extends Error {
message: "The 'declare' modifier is only allowed when the 'allowDeclareFields' option of @babel/plugin-transform-flow-strip-types or @babel/preset-flow is enabled.";
}The plugin recognizes and processes the following Flow directive patterns:
@flow - Standard Flow directive@flow strict - Strict Flow checking@flow strict-local - Local strict Flow checking@flow weak - Weak Flow checking@noflow - Disable Flow checkingUsage Examples:
// Input with Flow directive
// @flow
function add(a: number, b: number): number {
return a + b;
}
// Output (directive removed, types stripped)
function add(a, b) {
return a + b;
}Runtime Dependencies:
@babel/helper-plugin-utils - Provides the declare function for plugin creation@babel/plugin-syntax-flow - Enables Flow syntax parsingPeer Dependencies:
@babel/core ^7.0.0-0 - Required Babel core for plugin execution// Require Flow directive to be present
{
"plugins": [
["@babel/plugin-transform-flow-strip-types", {
"requireDirective": true
}]
]
}
// Allow declare fields (Babel 7 only)
{
"plugins": [
["@babel/plugin-transform-flow-strip-types", {
"allowDeclareFields": true
}]
]
}
// Combined with other plugins
{
"plugins": [
"@babel/plugin-syntax-flow",
"@babel/plugin-transform-flow-strip-types",
"@babel/plugin-transform-class-properties"
]
}