myHotTake

Tag: TypeScript Babel integration

  • How to Set Up TypeScript with Babel for JavaScript Projects

    If you find this story helpful, feel free to like or share it with others who might enjoy it too!


    I am a baker in a world where not everyone can handle gluten. TypeScript is my special flour that gives my cakes a nice structure and firmness, ensuring they don’t fall apart. But here’s the catch: not everyone can digest this flour directly, just like some browsers can’t understand TypeScript.

    So, I need a sifter—let’s call it Babel—that can transform my special flour into something that everyone can enjoy without any trouble. Babel is like my trusty assistant in the bakery. It takes in the special flour and sifts it down into a more universally digestible form—JavaScript. This makes my cakes accessible to everyone in town, regardless of their gluten sensitivity.

    To set up my bakery to use this process, I first ensure that I’ve got my TypeScript flour ready. I gather my ingredients by installing the necessary packages: TypeScript itself and Babel, along with a few plugins like @babel/preset-typescript to ensure the flour can be transformed correctly. I also set up my baking station—my babel.config.js file—to make sure Babel knows how to handle the TypeScript flour.

    With everything in place, I start mixing the ingredients. I write my recipes in TypeScript, confident that they will make sturdy, well-structured cakes. When it’s time to bake, Babel steps in, sifting through the TypeScript and converting it into a form that my oven—any browser or JavaScript environment—can handle perfectly.

    And just like that, my cakes are ready to be enjoyed by everyone, no matter their dietary restrictions. Through this process, I ensure that my bakery can serve delicious, perfectly structured cakes to all my customers, thanks to the incredible teamwork between TypeScript and Babel.


    In our bakery, setting up the workspace is akin to setting up our project environment. First, we need to install the necessary ingredients. Here’s how we do it in code:

    npm install --save-dev typescript @babel/core @babel/preset-env @babel/preset-typescript

    These packages are like stocking up on our special flour and sifter tools.

    Next, we need to set up our babel.config.js file—this is our recipe book that guides Babel on how to transform our TypeScript flour into digestible JavaScript. It might look like this:

    module.exports = {
      presets: [
        '@babel/preset-env',
        '@babel/preset-typescript'
      ]
    };

    This configuration file tells Babel to use both the @babel/preset-env to ensure the JavaScript is compatible with the ovens (browsers/environment), and @babel/preset-typescript to handle the special TypeScript flour.

    For our TypeScript configuration, we might have a tsconfig.json file—a checklist for ensuring our ingredients are in order:

    {
      "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "strict": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true
      },
      "include": ["src"]
    }

    This file ensures that when we write our recipes (code) in TypeScript, they adhere to the structure and standards we need for successful baking (transpilation).

    Now, to run the kitchen (our development), we create a build script in our package.json:

    "scripts": {
      "build": "babel src --extensions '.ts' --out-dir dist"
    }

    This script is like turning on the mixer, allowing Babel to take everything in our src folder, sift through it, and output the transformed JavaScript into a dist folder.

    Key Takeaways:

    • TypeScript provides structure and type safety in our code, much like how special flour gives structure to cakes.
    • Babel acts as the sifter, converting TypeScript into JavaScript so it can be universally understood.
    • Setting up TypeScript with Babel involves configuring both babel.config.js and tsconfig.json to ensure seamless transpilation.
    • The build script in package.json orchestrates the transformation process, akin to starting the mixer in our bakery.