Watching Files with tsc --watch
What you'll learn: How to let TypeScript automatically recompile your code whenever you save changes, eliminating manual recompilation.
The Manual Recompilation Problem
So far, you've been running tsc each time you modify a .ts file. Edit your code, save, switch to terminal, run tsc, check output—it's tedious and breaks your flow. During active development, you might change files dozens of times per hour.
Enter Watch Mode
TypeScript's watch mode solves this by monitoring your source files for changes. When you run:
tsc --watch
...or the shorthand:
tsc -w
The compiler starts, compiles your project, then keeps running in the background. It watches all files matched by your tsconfig.json (based on include and exclude patterns). Whenever you save a .ts file, tsc detects the change and automatically recompiles just the affected files.
What It Looks Like
After running tsc --watch, you'll see output like:
[12:30:45 PM] Starting compilation in watch mode...
[12:30:47 PM] Found 0 errors. Watching for file changes.
Now edit a TypeScript file and save. The terminal immediately shows:
[12:30:52 PM] File change detected. Starting incremental compilation...
[12:30:53 PM] Found 0 errors.
The process stays alive until you manually stop it (Ctrl+C).
Why This Matters
Watch mode creates a faster feedback loop. You write code, save, and instantly see if you have type errors—no context switching required. Combined with tools like nodemon or browser auto-refresh, you can build a smooth development workflow where changes appear almost immediately.
Key Takeaway: Use tsc --watch during development to automatically recompile TypeScript files whenever they change, keeping your compiled JavaScript in sync without manual intervention.