Sub topic 9
Rebuilding on Change with Watch Mode
Prerequisites and the feedback loop
This chapter assumes you know JavaScript functions and modules, can use a terminal, and have a Node.js project with TypeScript installed. It also assumes your project has a tsconfig.json file that defines the source and output folders.
Watch mode keeps TypeScript running while you develop. Instead of manually starting a build after every edit, you start the compiler once. It watches the files included by your project configuration. When a file changes, TypeScript checks the project again and emits updated JavaScript when appropriate.
The basic command is:
npx tsc --watchYou can use the shorter form:
npx tsc -wThe terminal stays occupied while the compiler watches. You will see a message when the initial build finishes and another message after each rebuild. Press Ctrl+C to stop the watcher.
Watch mode uses your project configuration
When you run npx tsc --watch without file names, TypeScript searches for tsconfig.json in the current directory. The compiler uses that file to determine which sources to include and where to write output.
For example:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"sourceMap": true,
"skipLibCheck": true
},
"include": ["src"]
}With this configuration, changing src/index.ts causes TypeScript to check the included project. If there are no errors, the corresponding JavaScript file and source map are written under dist.
A common mistake is to start watch mode with a file argument:
npx tsc src/index.ts --watchThis form changes how TypeScript chooses its input and may cause it to ignore the project configuration. Use the project command instead:
npx tsc --watchIf the configuration has a different name or location, provide it with --project:
npx tsc --project config/tsconfig.json --watchWatch mode and errors
Watch mode does not stop permanently after a type error. It reports the error and continues watching. After you fix the source, it checks the project again.
Consider this file:
function firstLetter(text: string | undefined): string {
return text.charAt(0);
}With strict enabled, TypeScript reports that text might be undefined. Watch mode displays the diagnostic immediately after the file is saved. Correct the function by checking the value:
function firstLetter(text: string | undefined): string {
return text === undefined ? "" : text.charAt(0);
}The next rebuild should complete without that error.
Type-checking without rebuilding
Some projects use a bundler or runtime tool to produce JavaScript. In that setup, TypeScript can check the code while another tool handles transformation and bundling.
Use watch mode with --noEmit to check files continuously without writing JavaScript:
npx tsc --noEmit --watchThis is useful when a development server already rebuilds the application. It prevents two tools from producing competing output while still showing type errors quickly.
Use ordinary watch mode when tsc owns the build output. Use --noEmit --watch when another tool owns that output.
Exercise
Your project has a bundler that watches source files and writes the application bundle. Which command should you use to run TypeScript checks without creating a second set of JavaScript files?
Show answer
Run npx tsc --noEmit --watch.