Strip flow type annotations from your output code.
npx @tessl/cli install tessl/npm-babel-plugin-transform-flow-strip-types@6.22.0babel-plugin-transform-flow-strip-types is a Babel plugin that strips all Flow type annotations and declarations from your JavaScript code during compilation. It removes Flow-specific syntax while preserving the original JavaScript functionality, making it essential for projects that use Flow for type checking in development but need clean JavaScript output for production.
npm install --save-dev babel-plugin-transform-flow-strip-types// Default export - the plugin factory function
const flowStripTypes = require("babel-plugin-transform-flow-strip-types");ES6 import:
import flowStripTypes from "babel-plugin-transform-flow-strip-types";Note: The plugin exports a single default function that serves as the plugin factory.
{
"plugins": ["transform-flow-strip-types"]
}babel --plugins transform-flow-strip-types script.jsconst babel = require("babel-core");
const result = babel.transform(sourceCode, {
plugins: ["transform-flow-strip-types"]
});The plugin operates as part of the Babel AST transformation pipeline. It inherits Flow syntax parsing capabilities from babel-plugin-syntax-flow and implements visitor methods that traverse the AST to remove Flow-specific nodes while preserving JavaScript functionality.
The main export is a function that creates a Babel plugin when provided with Babel's types object.
/**
* Creates a Babel plugin that strips Flow type annotations
* @param {Object} babel - Babel plugin options object (automatically provided by Babel)
* @param {Object} babel.types - Babel types helper object (t)
* @returns {Object} Babel plugin configuration object with inherits and visitor properties
*/
function flowStripTypesPlugin({ types: t }) {
return {
inherits: BabelPlugin,
visitor: VisitorObject
};
}Usage Example:
// Typically used automatically by Babel when plugin is configured
const plugin = require("babel-plugin-transform-flow-strip-types");
// Manual plugin instantiation (rarely needed)
const babel = require("babel-core");
const babelTypes = require("babel-types");
const pluginInstance = plugin({ types: babelTypes });
// Plugin is applied during Babel transformation process
const result = babel.transform(code, {
plugins: [plugin] // Babel handles plugin instantiation automatically
});The plugin factory returns a Babel plugin configuration object with the following structure:
interface BabelPlugin {
/** Inherits Flow syntax parsing from babel-plugin-syntax-flow */
inherits: BabelPlugin;
/** AST visitor methods for transforming Flow syntax */
visitor: {
/** Handles program-level directives and comments */
Program: (path: NodePath<Program>, state: PluginPass) => void;
/** Removes Flow type declarations and annotations */
Flow: (path: NodePath<FlowType>) => void;
/** Handles class property type annotations and variance */
ClassProperty: (path: NodePath<ClassProperty>) => void;
/** Removes class implements clauses and processes properties */
Class: (path: NodePath<Class>) => void;
/** Removes optional parameter annotations */
AssignmentPattern: (opts: { node: Node }) => void;
/** Removes optional parameter flags from function parameters */
Function: (opts: { node: Node }) => void;
/** Unwraps type cast expressions to their inner expressions */
TypeCastExpression: (path: NodePath<TypeCastExpression>) => void;
};
}
interface NodePath<T = Node> {
node: T;
remove(): void;
replaceWith(node: Node): void;
get(key: string): NodePath | NodePath[];
}
interface PluginPass {
file: {
ast: {
comments: Comment[];
};
};
}
interface Comment {
value: string;
ignore?: boolean;
}The plugin handles the following Flow syntax transformations:
Removes or cleans up @flow directive comments from the top of files.
Input:
// @flow
function foo() {}Output:
function foo() {}Removes type annotations from function parameters, return types, and variable declarations.
Input:
function foo(one: any, two: number, three?): string {}
const x: number = 5;Output:
function foo(one, two, three) {}
const x = 5;Removes type annotations from class properties, implements clauses, and variance annotations.
Input:
class Example implements SomeInterface {
+property: string;
-readOnlyProp: number;
method(): void {}
}Output:
class Example {
method() {}
}Removes variance annotations (+ for covariant, - for contravariant) from class properties.
Input:
class Container<T> {
+covariant: T;
-contravariant: T;
invariant: T;
}Output:
class Container {
invariant;
}Removes Flow type aliases, interface declarations, and other Flow-specific declarations.
Input:
type MyType = string | number;
interface MyInterface {
prop: string;
}Output:
// These declarations are completely removedUnwraps type cast expressions to their underlying expressions.
Input:
const value = (someExpression: SomeType);Output:
const value = someExpression;Removes generic type parameters and qualified type references.
Input:
const a: Array<string> = [];
const b: A.B.C = value;Output:
const a = [];
const b = value;Removes tuple type annotations.
Input:
const tuple: [string, number] = ["hello", 42];Output:
const tuple = ["hello", 42];Handles optional parameters and default values with Flow types.
Input:
function foo(numVal?: number = 2) {}
function bar(str: string = "default") {}Output:
function foo(numVal = 2) {}
function bar(str = "default") {}Removes string literal type annotations.
Input:
const status: "pending" | "complete" = "pending";Output:
const status = "pending";Removes Flow declare statements entirely.
Input:
declare function foo(): void;
declare var x: number;
declare module "some-module" {
export function bar(): string;
}Output:
// These declarations are completely removedThe plugin does not throw errors during transformation. It silently removes Flow syntax and preserves valid JavaScript code. If the input contains invalid Flow syntax, the parsing error would occur at the Babel parser level before this plugin runs.
The plugin requires:
^6.18.0 - Provides Flow syntax parsing capabilities^6.22.0 - Babel runtime helpersbabel-plugin-syntax-flow version 6.18.0