myHotTake

Tag: tsconfig setup

  • How to Optimize TypeScript Builds with Incremental Compilation

    If you enjoy this story and find it helpful, feel free to like or share it!


    I’m a book editor working on a series of novels. Every time the author submits a new draft, I don’t want to reread the entire series to find the changes; instead, I want to focus just on the new chapters or any revised sections. This way, I can efficiently keep the entire series polished without wasting time on parts that haven’t changed.

    In the world of TypeScript, this is similar to how I configure the TypeScript compiler for incremental builds. Think of each chapter of the book as a file in my TypeScript project. When I enable incremental builds, it’s like setting up a system where I only review the chapters that have been updated or are new. This is done by creating a sort of “memory” of past edits, which in TypeScript terms is a cache of previous compilations.

    To set this up, I dive into my tsconfig.json file—this is like my editor’s notebook where I jot down important guidelines for my editing process. Here, I add "incremental": true, which is equivalent to writing a note to myself: “Remember changes between drafts.” I also specify a "tsBuildInfoFile" to store this memory, ensuring I know exactly where to look when I pick up the next draft.

    Every time the author gives me a new version, my editing process swiftly skips over unchanged chapters, allowing me to focus my energy and attention on only what’s new or modified. This efficiency means I can keep up with the fast pace of submissions without getting overwhelmed. So, just like my editing system helps me manage novels effectively, incremental builds keep my TypeScript projects running smoothly and efficiently.


    In the world of TypeScript, our tsconfig.json file is like my editor’s notebook. By adding "incremental": true, I’m telling the TypeScript compiler to remember past compilations, similar to how I remember past edits in the book series. Here’s how I set it up:

    {
      "compilerOptions": {
        "incremental": true,
        "tsBuildInfoFile": "./.tsbuildinfo",
        "outDir": "./dist"
      },
      "include": ["src/**/*"]
    }

    In this configuration:

    • "incremental": true is like making a note to remember changes between drafts, so I don’t have to start fresh each time.
    • "tsBuildInfoFile": "./.tsbuildinfo" specifies where this memory or cache is stored, just like keeping an organized file of past edits.
    • "outDir": "./dist" tells the compiler where to put the compiled JavaScript files, akin to deciding where the final edited chapters will be stored.

    When I run the TypeScript compiler using tsc, it now uses this setup to only recompile files that have changed or have dependencies that changed. This is like focusing only on new or revised chapters, rather than rereading the whole series. This process can significantly speed up build times, especially in large projects.

    Key Takeaways

    1. Incremental Builds: By enabling incremental builds in TypeScript, we can improve the efficiency of our build process by reusing information from previous compilations.
    2. Configuration Matters: Setting "incremental": true in tsconfig.json and specifying a "tsBuildInfoFile" are essential steps to enable this feature.
    3. Efficiency in Large Projects: Just as I save time by only editing new or changed chapters, incremental builds allow developers to focus on modified files, speeding up the development process.
    4. Practical Use: This setup is particularly useful for large projects where recompiling everything from scratch would be time-consuming.
  • How to Seamlessly Blend JavaScript and TypeScript Projects

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


    I’m a detective in a city, where two neighboring districts exist: JavaScript Junction and TypeScript Town. These districts have their own unique charm, much like coffee and tea. I love coffee for its boldness and unpredictability, just like JavaScript, where anything goes, and spontaneity rules the day. But sometimes, I crave the structure and predictability of tea, which reminds me of TypeScript, where rules and clarity guide my investigations.

    One day, I decide to open a detective agency that caters to the entire city, allowing clients from both JavaScript Junction and TypeScript Town to come to me with their mysteries. To make this work, I need to create a headquarters that welcomes residents from both districts seamlessly. This is where my configuration skills come into play.

    I start by setting up a main office in a neutral zone, ensuring my agency can handle clients who speak both JavaScript and TypeScript languages. I install a universal translation device—my ‘tsconfig.json’ file—which acts like a book that understands both dialects. Inside this book, I specify ‘allowJs’, which allows my agency to accept JavaScript clients without any fuss. I also ensure ‘checkJs’ is set to false, so JavaScript clients can feel free and unjudged, just like in their home district.

    To keep things organized, I map out the agency’s territory with ‘include’ and ‘exclude’ zones, ensuring I only take cases from areas I’m prepared to handle. This means setting boundaries on which files I want to process, making sure my agency remains efficient and effective.

    With everything in place, my detective agency thrives, seamlessly solving mysteries from both JavaScript Junction and TypeScript Town. By embracing the strengths of both districts, I’ve created a harmonious environment where everyone feels welcome and understood.

    And that’s how I configure my project to support a mix of JavaScript and TypeScript, much like running a detective agency that bridges the gap between two communities. If this story resonates with you, feel free to give it a thumbs up or share it!


    Setting Up Our Agency Headquarters (tsconfig.json)

    First, we set up our agency’s main configuration file, tsconfig.json, which helps us handle both JavaScript and TypeScript cases:

    {
      "compilerOptions": {
        "allowJs": true,       // Allows JavaScript files in our project
        "checkJs": false,      // Disables type checking for JavaScript files
        "target": "es6",       // Sets the ECMAScript target version
        "module": "commonjs",  // Sets the module system
        "outDir": "./dist",    // Specifies the output directory
        "strict": true,        // Enables all strict type-checking options for TypeScript
      },
      "include": ["src/**/*"], // Includes all files in the src directory
      "exclude": ["node_modules"] // Excludes the node_modules directory
    }

    Handling Cases from JavaScript Junction

    Here’s how we might handle a JavaScript case in our agency. we’re investigating a mysterious function that calculates the area of a rectangle:

    // rectangleArea.js
    function calculateArea(length, width) {
      return length * width;
    }
    
    console.log(calculateArea(5, 10)); // Outputs: 50

    With our configuration, we can include this JavaScript file in our project without any issues, thanks to allowJs.

    Solving Mysteries in TypeScript Town

    For a TypeScript case, let’s enhance our investigation with type safety:

    // rectangleArea.ts
    function calculateArea(length: number, width: number): number {
      return length * width;
    }
    
    console.log(calculateArea(5, 10)); // Outputs: 50
    // console.log(calculateArea("5", 10)); // This would cause a TypeScript error

    By specifying types, we prevent potential errors, ensuring our investigation remains on track.

    Final Thoughts

    By configuring TypeScript to support mixed JavaScript and TypeScript projects, I’ve created a detective agency that can tackle a diverse range of cases. Here’s what we learned:

    • Flexibility: allowJs lets us include JavaScript files, while checkJs ensures they aren’t type-checked.
    • Organization: Using include and exclude helps us manage our files efficiently.
    • Safety: TypeScript’s type system helps catch errors early, making our investigations more reliable.