Babel Plugin for TypeScript Transformation
This package, `@babel/plugin-transform-typescript`, is a core Babel plugin designed to strip TypeScript type annotations from code, transforming it into standard ECMAScript. It operates solely on the syntax level and does not perform any type-checking; users must integrate the TypeScript compiler (tsc) separately for type validation. The current stable major version is 7 (e.g., v7.29.2 as of March 2026), with active development ongoing for Babel 8, which is currently in release candidate stages (e.g., v8.0.0-rc.3). Babel maintains a frequent release cadence for patch versions and rolls out minor/major updates periodically. A key differentiator is its focus on pure syntax transformation, which makes it faster than a full TypeScript compilation but also means it explicitly does not support TypeScript-specific features like `namespace` declarations, `const enum`s, or the legacy `export =` and `import =` syntax, as these features require type information for meaningful transformation.
Common errors
-
Error: Cannot find plugin '@babel/plugin-transform-typescript'. Make sure you have installed it properly and are passing it a valid path.
cause The plugin package is not installed or incorrectly referenced in the Babel configuration.fixEnsure `@babel/plugin-transform-typescript` is installed (`npm install --save-dev @babel/plugin-transform-typescript`) and correctly specified in your `babel.config.js` or `.babelrc` (e.g., `plugins: ['@babel/plugin-transform-typescript']`). -
TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
cause This is a TypeScript type error, which Babel does not catch because it only strips types and does not perform type checking.fixRun `tsc --noEmit` as part of your build process or ensure your IDE/editor is configured to show TypeScript errors. Correct the type mismatch in your source code. -
SyntaxError: 'namespace' is not supported by Babel or 'const enum' is not supported by Babel
cause Attempting to use TypeScript features explicitly unsupported by `@babel/plugin-transform-typescript`.fixRefactor your code to use ES Module `import`/`export` instead of `namespace`, and use standard `enum`s or plain JavaScript objects/literals instead of `const enum`s.
Warnings
- breaking When upgrading to Babel 8 (currently in release candidate), `@babel/plugin-transform-typescript` drops support for TypeScript's legacy `module <identifier>` syntax. This is part of a broader push to align with modern ES Modules.
- gotcha This plugin *only* strips TypeScript type annotations; it does not perform type checking. Code that is syntactically valid but contains type errors will still be transpiled without error. You must run the TypeScript compiler (tsc) separately for full type validation.
- gotcha The plugin explicitly does not support certain TypeScript-specific features like `namespace` declarations, `const enum`s, or the legacy `export =` and `import =` syntax. These features require type information to transpile correctly, which Babel's type-stripping approach does not provide.
- breaking Upgrading to Babel 8 generally introduces several breaking changes across the Babel ecosystem, including changes in `babel-parser`, `@babel/code-frame`, and `@babel-eslint-parser`. While this plugin's specific API might remain stable, overall Babel configuration and integration may require significant updates.
Install
-
npm install babel-plugin-transform-typescript -
yarn add babel-plugin-transform-typescript -
pnpm add babel-plugin-transform-typescript
Imports
- Config via plugins array
{ "plugins": [ "babel-plugin-transform-typescript" // Older, unscoped name ] }{ "plugins": [ "@babel/plugin-transform-typescript" ] } - Programmatic usage
import { plugin } from '@babel/plugin-transform-typescript';const transform = require('@babel/core').transformSync; const plugin = require('@babel/plugin-transform-typescript'); const code = `const x: number = 0;`; const output = transform(code, { plugins: [plugin] }).code; - CLI usage
babel --plugins transform-typescript src/index.ts
babel --plugins @babel/plugin-transform-typescript src/index.ts --out-file dist/index.js
Quickstart
/* file: package.json */
{
"name": "my-ts-project",
"version": "1.0.0",
"scripts": {
"build": "babel src --out-dir dist --extensions \".ts,.tsx\""
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/plugin-transform-typescript": "^7.0.0"
}
}
/* file: babel.config.js */
module.exports = {
plugins: [
"@babel/plugin-transform-typescript",
// If you use React and JSX, also add:
// "@babel/plugin-transform-react-jsx"
]
};
/* file: src/index.ts */
interface User {
id: number;
name: string;
email?: string;
}
const user: User = {
id: 1,
name: "Alice",
email: "alice@example.com"
};
function greet(person: User): string {
return `Hello, ${person.name}! Your ID is ${person.id}.`;
}
console.log(greet(user));
// Example of a type-only import (will be stripped by Babel)
import type { UtilityType } from './types';
// A type declaration
type UtilityType = { timestamp: number };
const data: UtilityType = { timestamp: Date.now() };
console.log(`Current data timestamp: ${data.timestamp}`);
/* To run this example: */
/* 1. Create the files above */
/* 2. `npm install` */
/* 3. `npm run build` */
/* 4. `node dist/index.js` */