liveBook · Manning

Watch mode

As explained in Essential TypeScript 5, Third Edition

Watch mode is a compiler or test-runner operating mode that monitors files and reruns processing when changes are detected.

Watch mode removes the need to restart a compiler or test runner after each edit. The TypeScript compiler monitors the project and recompiles when files change; Jest reruns unit tests when tests or application code change. section 5.5.2 section 6.4.3

Listing 5.12 Starting the compiler in watch mode
tsc --watch

Run `tsc --watch` from the project folder to compile once and keep watching. Saving a change triggers another compilation; the compiler reports errors or confirms that it found none. section 5.5.2

Compilation and execution are separate. `tsc --watch` does not run the generated JavaScript: run it separately with Node.js, or use `tsc-watch --onsuccess "node dist/index.js"` to execute it after successful compilation. A package.json `start` script can hold that command so `npm start` starts the workflow. Avoid launching Node.js merely because an output file changed: generated files are not necessarily written at the same time. section 5.5.2 section 7.1

Listing 6.17 Starting the unit test framework in watch mode
npx jest --watchAll

Run `npx jest --watchAll` in a second prompt to keep unit-test results visible alongside compiler output. Jest reports failures and reruns tests after changes; its watch controls can narrow runs to failed tests or tests related to changed files. section 6.4.3

If compiler watch mode stops with `Check failed: U_SUCCESS(status)`, updating Node.js may resolve the problem. The watch process is a development convenience, not a replacement for checking whether compilation succeeded or whether tests passed. section 5.5.2 section 6.4.3

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Shows commands, change-triggered output, and the distinction between compilation and execution, without explaining file-monitoring internals.
Examples
Uses `tsc --watch`, a `tsc-watch` start script, and `jest --watchAll` with passing and failing test reports.
What it assumes
Assumes a TypeScript project with NPM, while giving the commands needed to start compiler and test watch modes.
What you can do after
Operate/configure it with compiler, package-script, and Jest commands to get feedback after edits and run compiled code when appropriate.
How it connects
Connects watch mode to TypeScript compilation, generated JavaScript, Node.js execution, package.json scripts, and Jest unit tests.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.