Core transformation function for removing Flow type annotations from JavaScript source code with configurable options and source map support.
Main transformation function that processes JavaScript source code containing Flow type annotations.
/**
* Remove Flow type annotations from JavaScript source code
* @param {string} source - JavaScript source code with Flow annotations
* @param {TransformOptions} options - Configuration options
* @returns {TransformResult} Result object with toString() and generateMap() methods
*/
function flowRemoveTypes(source, options);
interface TransformOptions {
/** Transform all files, not just those with @flow pragma (default: false) */
all?: boolean;
/** Remove types completely rather than replacing with spaces (default: false) */
pretty?: boolean;
/** Remove uninitialized class fields completely (default: false) */
ignoreUninitializedFields?: boolean;
/** Remove empty import statements after type removal (default: false) */
removeEmptyImports?: boolean;
}
interface TransformResult {
/** Returns the transformed source code as a string */
toString(): string;
/** Returns a v3 source map object */
generateMap(): SourceMap;
}Usage Examples:
const flowRemoveTypes = require('flow-remove-types');
const fs = require('fs');
// Basic transformation
const input = fs.readFileSync('app.js', 'utf8');
const result = flowRemoveTypes(input);
console.log(result.toString());
// Transform with pretty output and source maps
const result = flowRemoveTypes(input, {
pretty: true,
all: true
});
fs.writeFileSync('output.js', result.toString());
fs.writeFileSync('output.js.map', JSON.stringify(result.generateMap()));
// Remove empty imports and uninitialized fields
const result = flowRemoveTypes(input, {
removeEmptyImports: true,
ignoreUninitializedFields: true
});The result object returned by flowRemoveTypes() provides two methods for accessing the transformed code and source maps.
/**
* Returns the transformed source code as a string
* @returns {string} Transformed JavaScript source code
*/
toString(): string;
/**
* Returns a v3 source map for debugging transformed code
* @returns {SourceMap} Source map object compatible with debugging tools
*/
generateMap(): SourceMap;By default, Flow Remove Types replaces type annotations with whitespace to maintain exact character positioning:
Input:
function add(a: number, b: number): number {
return a + b;
}Output:
function add(a , b ) {
return a + b;
}With pretty: true, types are removed completely but source maps are required for debugging:
Input:
function add(a: number, b: number): number {
return a + b;
}Output:
function add(a, b) {
return a + b;
}Without all: true, only files with Flow pragmas are transformed:
// @flow
// This file will be transformed
// @noflow
// This file will be transformed but types removed
// No pragma - file returned unchanged (unless all: true)interface SourceMap {
version: number;
sources: string[];
names: string[];
mappings: string;
file?: string;
sourcesContent?: string[];
}
interface TransformError extends Error {
loc?: {
line: number;
column: number;
};
}Transform errors include location information when parsing fails:
try {
const result = flowRemoveTypes(invalidSource);
} catch (error) {
if (error.loc) {
console.log(`Syntax error at line ${error.loc.line}, column ${error.loc.column}`);
}
console.log(error.message);
}