Babel TypeScript Preset

7.0.0-alpha.19 · active · verified Sun Apr 19

`@babel/preset-typescript` is a Babel preset specifically designed to strip TypeScript syntax from source code, converting it into standard JavaScript. It does not perform any type checking; its sole purpose is to remove type annotations, enums, and other TypeScript-specific constructs so that Babel can continue processing the resulting JavaScript. This allows developers to leverage Babel's extensive plugin ecosystem for further transformations, such as targeting older JavaScript environments with `@babel/preset-env` or integrating JSX transformations. The package is part of the Babel monorepo, maintaining an active release schedule with frequent patch and minor updates (often bi-weekly) and new major versions released periodically. As of April 2026, the `7.x` series is stable, with `7.29.2` being a recent patch, while the `8.x` series is in a release candidate phase, with `8.0.0-rc.3` being the latest. Its primary differentiator is enabling a unified transformation pipeline for projects using TypeScript in conjunction with various Babel features that the TypeScript compiler alone does not provide.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `@babel/preset-typescript` with `@babel/core` to strip TypeScript syntax and transpile to JavaScript, showing a simple class and type annotation.

import * as babel from '@babel/core';

const typescriptCode = `
interface Shape { 
  area(): number;
}

class Circle implements Shape {
  constructor(public radius: number) {}
  area(): number {
    return Math.PI * this.radius * this.radius;
  }
}

const myCircle: Circle = new Circle(10);
console.log('Circle area:', myCircle.area());

const greeting: string = 'Hello Babel!';
console.log(greeting);
`;

async function transformTsCode() {
  try {
    const result = await babel.transformAsync(typescriptCode, {
      filename: 'example.ts', // Essential for Babel to correctly apply TS preset
      presets: [
        ['@babel/preset-typescript', {
          // Example option: allows properties of classes to be declared without an initializer
          allowDeclareFields: true
        }],
        ['@babel/preset-env', {
          // Transpile for the current Node.js version
          targets: { node: 'current' }
        }]
      ]
    });

    console.log('--- Original TypeScript Code ---\n', typescriptCode);
    console.log('\n--- Transformed JavaScript Code (via Babel) ---\n', result?.code);
  } catch (error) {
    console.error('Babel transformation failed:', error);
  }
}

transformTsCode();

view raw JSON →