- TypeScript Tutorial
- TypeScript Home
- TypeScript Introduction
- TypeScript Setup
- TypeScript First Program
- TypeScript vs JavaScript
- TypeScript Data Types
- TypeScript Type Inference
- TypeScript Type Annotations
- TypeScript Interfaces
- TypeScript Enums
- TypeScript Type Aliases
- TypeScript Type Assertions
- TypeScript Variables
- TypeScript Functions
- TypeScript Functions
- TypeScript Optional Parameters
- TypeScript Default Parameters
- TypeScript Rest Parameters
- TypeScript Arrow Functions
- Classes and Objects
- Introduction to Classes
- Properties and Methods
- Access Modifiers
- Static Members
- Inheritance
- Abstract Classes
- Interfaces vs Classes
- Advanced Types
- TypeScript Union Types
- TypeScript Intersection Types
- TypeScript Literal Types
- TypeScript Nullable Types
- TypeScript Type Guards
- TypeScript Discriminated Unions
- TypeScript Index Signatures
- TypeScript Generics
- Introduction to Generics
- TypeScript Generic Functions
- TypeScript Generic Classes
- TypeScript Generic Constraints
- TypeScript Modules
- Introduction to Modules
- TypeScript Import and Export
- TypeScript Default Exports
- TypeScript Namespace
- Decorators
- Introduction to Decorators
- TypeScript Class Decorators
- TypeScript Method Decorators
- TypeScript Property Decorators
- TypeScript Parameter Decorators
- Configuration
- TypeScript tsconfig.json File
- TypeScript Compiler Options
- TypeScript Strict Mode
- TypeScript Watch Mode
TypeScript Watch Mode
TypeScript's watch mode is a powerful productivity feature that allows the TypeScript compiler (tsc) to continuously monitor your source files. Instead of you manually running a build command every time you fix a typo or add a function, TypeScript detects the save event and automatically triggers a recompile. This creates a tight feedback loop, allowing you to see errors or updated JavaScript output almost instantly.
Key Features of Watch Mode
Automatic Compilation on File Changes
- The moment you hit
Ctrl+S(orCmd+S), TypeScript identifies which file changed and begins the compilation process immediately. - Benefit: It removes the "context switching" of moving from your editor to the terminal, keeping you in the "flow state" longer.
File Watching
- The compiler doesn't just look at one file; it monitors the entire scope of your project as defined by your configuration. If you add a new file to a watched folder, TypeScript picks it up automatically.
- Benefit: You don't have to restart the compiler when you expand your project structure.
Incremental Compilation
- This is the secret sauce of TypeScript performance. Instead of rebuilding your entire application from scratch, TypeScript only recompiles the files that changed and updates the parts of the dependency graph affected by those changes.
- Benefit: On large projects with hundreds of files, this makes the difference between a 10-second wait and a sub-second update.
Supports Projects with Multiple Files
- Whether you are writing a simple script or a massive enterprise monorepo, Watch Mode scales. When paired with a
tsconfig.json, it respects all your path aliases, inclusions, and exclusions. - Benefit: Ensures that your development environment behaves exactly like your production build environment.
How to Enable Watch Mode
To start the compiler in watch mode for a single specific file, use the following command in your terminal:
tsc --watch
However, in most professional workflows, you will be working within a project folder that contains a tsconfig.json file. In this case, you can use the shorthand flag:
tsc -w
Once executed, the terminal will stay active and display a message like: "Compilation complete. Watching for file changes."
tsc -w and use a second tab for running their application or version control commands.
Example of Running TypeScript in Watch Mode
Simple Watch Mode Command:
tsc --watch
- TypeScript will scan the current directory. If it finds a
tsconfig.json, it watches the whole project; otherwise, it waits for you to specify files.
Watch Mode with tsconfig.json:
Using a configuration file is the standard way to manage complex projects. To start watching based on your config settings, simply run:
tsc -w
- This tells the compiler to look at your
includeandexcluderules and start monitoring the relevant source tree.
tsc -w will also run their code (e.g., starting a Node.js server). It doesn't! It only converts .ts to .js. You still need a separate process (like node or a browser refresh) to execute the resulting JavaScript.
Watch Mode with tsconfig.json Example
By fine-tuning your tsconfig.json, you can make Watch Mode even more efficient. For example, you can prevent TypeScript from wasting resources watching your "dist" folder or "node_modules".
Example tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"strict": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist"]
}
In this scenario, TypeScript will only "listen" for changes inside the src folder. It will ignore any changes in node_modules, which significantly improves performance since that folder can contain thousands of files.
Benefits of Watch Mode
- Real-Time Feedback
- If you make a type error, the terminal running the watch command will immediately log the error. You catch bugs the second they are written rather than five minutes later during a manual build.
- Improved Productivity
- By automating the repetitive task of compilation, you save seconds that add up to hours over the course of a project.
- Faster Iteration
- Modern web development relies on "Hot Module Replacement" (HMR). Watch Mode is the first step in that chain, ensuring your JS files are always ready for your dev server to pick up and display.
package.json to make this even easier: "dev:watch": "tsc -w". Then, you can just run npm run dev:watch to start your session.
Summary
TypeScript's watch mode is an essential tool for any developer looking to streamline their workflow. By using tsc -w, you delegate the burden of manual compilation to the compiler, allowing you to focus entirely on writing logic. It leverages incremental builds to keep performance high even in large codebases and provides the immediate feedback necessary for modern, fast-paced software development. Whether you're building a small utility or a complex web app, keeping Watch Mode active in your terminal is a best practice that ensures your JavaScript output never falls behind your TypeScript source.