DEV Community

Cover image for Typescript Transpiler Setup
Taki
Taki

Posted on

1

Typescript Transpiler Setup

To master working with TypeScript-compatible transpilers like Babel, SWC, and Sucrase, follow this step-by-step approach:


1. Understanding the Role of Transpilers

Transpilers convert TypeScript (TS) code into JavaScript (JS) that can run in different environments (browsers, Node.js). The main reasons for using a transpiler instead of tsc (TypeScript compiler) include:

  • Faster compilation (SWC and Sucrase are much faster than tsc).
  • More flexible configurations (especially Babel for advanced JavaScript transformations).
  • Compatibility with build tools like Webpack, Vite, and Esbuild.

2. Setting Up and Using Different Transpilers

A. Using Babel with TypeScript

Babel is widely used for JavaScript transformations and can be configured to work with TypeScript.

Steps to set up Babel for TypeScript

  1. Initialize a project
   mkdir babel-ts-project && cd babel-ts-project
   npm init -y
Enter fullscreen mode Exit fullscreen mode
  1. Install Babel and TypeScript dependencies
   npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-typescript
Enter fullscreen mode Exit fullscreen mode
  1. Create a Babel configuration file (babel.config.json)
   {
     "presets": ["@babel/preset-env", "@babel/preset-typescript"]
   }
Enter fullscreen mode Exit fullscreen mode
  1. Create a TypeScript file (index.ts)
   const greet = (name: string): string => {
       return `Hello, ${name}!`;
   };
   console.log(greet("TypeScript with Babel"));
Enter fullscreen mode Exit fullscreen mode
  1. Transpile the code using Babel
   npx babel index.ts --out-file index.js
Enter fullscreen mode Exit fullscreen mode
  1. Run the transpiled JavaScript
   node index.js
Enter fullscreen mode Exit fullscreen mode

Babel doesn't check types: Unlike tsc, Babel only removes TypeScript syntax but does not enforce type checking. You need to use tsc --noEmit separately for type checking.


B. Using SWC (Speedy Web Compiler)

SWC is a Rust-based transpiler that is significantly faster than Babel.

Steps to set up SWC for TypeScript

  1. Install SWC CLI and dependencies
   npm install --save-dev @swc/core @swc/cli
Enter fullscreen mode Exit fullscreen mode
  1. Create an SWC configuration file (.swcrc)
   {
     "jsc": {
       "parser": {
         "syntax": "typescript"
       },
       "target": "es2022"
     }
   }
Enter fullscreen mode Exit fullscreen mode
  1. Transpile the TypeScript file
   npx swc index.ts -o index.js
Enter fullscreen mode Exit fullscreen mode
  1. Run the output
   node index.js
Enter fullscreen mode Exit fullscreen mode

SWC is much faster than Babel and also supports type stripping. However, like Babel, it does not perform type checking.


C. Using Sucrase

Sucrase is a lightweight TypeScript transpiler optimized for development.

Steps to set up Sucrase

  1. Install Sucrase
   npm install --save-dev sucrase
Enter fullscreen mode Exit fullscreen mode
  1. Transpile TypeScript to JavaScript
   npx sucrase ./index.ts --transform-typescript --out-dir dist
Enter fullscreen mode Exit fullscreen mode
  1. Run the output
   node dist/index.js
Enter fullscreen mode Exit fullscreen mode

Sucrase is the fastest among the three but is designed only for development. It lacks many transformation features available in Babel or SWC.


3. Comparing Babel, SWC, and Sucrase

Feature Babel SWC Sucrase
Speed Slow Fast Very Fast
Type Removal
Type Checking ❌ (needs tsc --noEmit)
Advanced Transformations ⚠️ (limited)
Suitable for Production ❌ (dev only)

4. Type Checking Strategy

Since none of these transpilers perform type checking, you should use tsc separately:

npx tsc --noEmit
Enter fullscreen mode Exit fullscreen mode

This ensures type safety without slowing down transpilation.


5. Integration with Build Tools

Webpack + Babel

  1. Install required packages:
   npm install --save-dev babel-loader webpack webpack-cli @babel/core @babel/preset-env @babel/preset-typescript
Enter fullscreen mode Exit fullscreen mode
  1. Configure webpack.config.js:
   module.exports = {
     entry: "./src/index.ts",
     module: {
       rules: [
         {
           test: /\.ts$/,
           use: "babel-loader",
           exclude: /node_modules/,
         },
       ],
     },
     resolve: { extensions: [".ts", ".js"] },
     output: { filename: "bundle.js" },
   };
Enter fullscreen mode Exit fullscreen mode

Vite + SWC

  1. Install Vite with SWC:
   npm create vite@latest my-app --template react-ts
   cd my-app
   npm install
Enter fullscreen mode Exit fullscreen mode
  1. Vite automatically uses SWC for TypeScript transpilation.

Conclusion

  • Use Babel if you need advanced transformations and compatibility with plugins.
  • Use SWC if you want fast production-ready builds.
  • Use Sucrase if you need the fastest development transpilation.
  • Always use tsc --noEmit for type checking since these transpilers do not check types.

Postmark Image

20% off for developers shipping features, not fixing email

Build your product without worrying about email infrastructure. Our reliable delivery, detailed analytics, and developer-friendly API let you focus on shipping features that matter.

Start free

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️