How to compile TypeScript in the Watch Mode (Automatic Compilation)

Updated: January 7, 2024 By: Guest Contributor Post a comment

Overview

TypeScript, akin to a riverboat gamely navigating the mighty Mississippi, has a watch mode – an automatic tool that compiles your code as you change it, ensuring your journey from development to production is as smooth and swift as the currents themselves.

Introduction to TypeScript Watch Mode

Welcome, kindred coder, to a realm where vigilance is championed above all – the TypeScript watch mode. TypeScript is a language both rich and robust, a type-safe companion to JavaScript, the lifeblood of internet creations, mapping out possibilities as endless as the stars spotting the heavens. The watch mode is akin to having a diligent river pilot aboard, minutely observing the river’s changing conditions, always ready to adjust the course – or in our case, automatically compiling your TypeScript files upon detecting changes, shunting the need to manually command the compilation after each edit.

In this guide, we shall traverse this mighty concept together, exploring how to set TypeScript’s watchful eyes upon your project. From the simplest incantations to conjure it to life to more befuddling charms that beseech advanced sorcery with configuration, let us embark on this expedition together.

Starting with TypeScript Watch Mode

To initiate the watch mode, you must first ensure the presence of ‘typescript’ in your toolkit. If you’re yet to be furnished with it, the npm package manager, a trusty quartermaster that furnishes you with all manner of programming provisions, can be employed.

$ npm install -g typescript

With TypeScript now dutifully installed, inviting the watch mode to your service is as simple as issuing a single command.

$ tsc --watch

This command sets TypeScript’s vigilant watch over your project, its eyes skimming your code for alterations as assiduously as a card shark’s at a riverboat poker game. Upon any change, it compels the machinery to compile afresh, so your output reflects the very latest of your toils.

Configuring TypeScript for the Watch

The sprawling landscapes you write your code upon can sometimes demand a more nuanced approach, and in such cases, you have to whisper the precise instructions into TypeScript’s ear through a file of configurations known as ‘tsconfig.json’.

{
  "compilerOptions": {
    "outDir": "./dist",
    "strict": true,
    /* More options here */
  },
  "include": ["./src/**/*"],
  "watchOptions": {
    "watchFile": "useFsEvents"
  }
}

This configuration is akin to mapping the precise route your riverboat should take, setting markers along the Mississippi that guide it to the desired destination with unwavering precision.

Advanced Watching Scenarios

For those master pilots who wish to exercise even greater control, more complex configurations abound. You can, for example, instruct TypeScript to ignore certain tributary files or directories, much as a skilled captain sidesteps dangerous whirlpools or menacing river pirates.

{
  "compilerOptions": {
    "outDir": "./dist"
  },
  "exclude": ["./src/tests/**/*"],
  "watchOptions": {
    "watchFile": "useFsEvents"
  }
}

This will ensure TypeScript turns a blind eye to the specified directory, focusing its watchful gaze only on the paths where true treasure lies.

Integrating with Build Tools

A grand steamship requires a crew, and sometimes your TypeScript watch mode does as well. Should you wish to integrate with task runners like Gulp or module bundlers like Webpack, you will find that they augment the capabilities of your project, each contributing their own kind of steam to your engine.

Conclusion

With the guidance provided, may your journey through TypeScript’s automatic compilation in watch mode be as majestic as a starlit cruise down the greatest of America’s rivers. Employ this tool wisely, and your development work will flow with the grace and certainty of the unceasing Mississippi itself – deep, powerful, and resolute.