Use this reference to design watch, execution, and restart scripts. Treat type-checking, JavaScript emission, execution, and process supervision as separate jobs even when one command coordinates them.
| Goal | Default tool | Important behavior |
|---|---|---|
| Report type errors only | tsc --noEmit --watch |
Uses the project compiler without executing code. |
| Run source with minimum latency | tsx watch |
Transforms and restarts quickly but does not type-check. |
| Run only successfully emitted JavaScript | tsc-watch --onSuccess |
Restarts the child only after a successful compilation. |
| Build a library or referenced workspace | tsc -b --watch |
Preserves declaration and project-reference build semantics. |
Assign process restart ownership to exactly one tool. Do not let tsx watch, tsc-watch, and another supervisor all restart the same program. If tsx owns execution, run tsc --noEmit --watch separately for diagnostics and keep the non-watch typecheck mandatory in CI. Use the project's existing multi-process runner rather than adding one by default.
Keep scripts explicit. This single example presents alternatives; choose one dev:* owner rather than running both:
{
"scripts": {
"dev:source": "tsx watch --clear-screen=false src/server.ts",
"dev:compiled": "tsc-watch --noClear --onSuccess \"node --enable-source-maps dist/server.js\"",
"typecheck": "tsc --noEmit",
"typecheck:watch": "tsc --noEmit --watch --preserveWatchOutput"
}
}Use project-local dependencies and the repository's package manager. Do not depend on globally installed typescript, tsx, or tsc-watch versions.
- Use
tsxfor applications, scripts, tests, and CLIs where direct source execution improves iteration speed. - Pair it with
tsc --noEmit;tsxuses esbuild and intentionally performs no type-checking. - Use
tsx watch entry.tsfor imported source dependencies. Add--includeonly for relevant non-imported inputs such as configuration or templates, and--excludegenerated or noisy paths. Quote globs so the shell does not expand them. - Use
--clear-screen=falsewhen persistent diagnostics and application logs matter. - Pass
--tsconfigwhen the runtime entry needs a non-default project configuration. - Prefer
node --import tsx entry.tswhen another Node flag or Node's CLI is the primary interface. - Do not use
tsxfor compiler-specific emit requirements that esbuild cannot reproduce, such asemitDecoratorMetadata. Confirm supportedtsconfigbehavior before relying on a compiler option at runtime.
- Use plain
tsc --watchwhen only compiler diagnostics or emit are needed. Addtsc-watchwhen compilation lifecycle hooks must control another command. - Use
--onSuccessto gate a runtime process on a clean compilation. Do not substitute--onEmit: emission can occur even when diagnostics fail unless the project prevents it. - Set
noEmitOnError: true, a deliberaterootDir/outDir, andsourceMap: truewhen running emitted JavaScript. Enable Node source maps for useful TypeScript stack traces. - Keep hook commands single-purpose. Put multi-step behavior in a named package script instead of embedding shell command chains in the hook.
- Let
tsc-watchterminate its previous child before starting the replacement. Make the application handleSIGTERMand close servers, database clients, workers, and telemetry promptly. - Decide whether the last successful process should remain active while a later compilation has errors; that is the normal
--onSuccessdevelopment model. Use a different supervisor policy if stale-but-working code must stop on failure. - Use
--compileCommandonly when intentionally selecting a compiler executable other than the normal localtsc; do not copy obsolete--compilerexamples.
Prefer native file-system events. Configure TypeScript watchOptions only when containers, network volumes, editor write patterns, or resource usage justify it. Polling is often more reliable on unusual file systems but costs more CPU.
Exclude emitted output, dependency directories, caches, coverage, and large generated trees from compiler watches. Keep .tsbuildinfo in an intentional cache/output location and out of version control unless the project has a specific reason to commit it. Ensure an output write cannot trigger another build or restart loop.
- Match
moduleandmoduleResolutionto the deployed Node or bundler runtime. A development loader must not conceal invalid production import extensions, package exports, or ESM/CJS behavior. - Remember that TypeScript
pathschecks imports but does not rewrite emitted specifiers. Verify the production runtime or bundler resolves every alias. - For libraries, run the declaration-producing build and inspect its output;
tsxexecution is not a substitute for declaration emit. - Test the actual production entry after a clean build. In CI, run at least the non-watch typecheck, tests, build, and a representative production-runtime check.
Introduce a temporary type error and confirm the chosen policy. Then make a valid runtime edit and confirm exactly one restart. Finally, trigger rapid consecutive edits, inspect TypeScript-mapped stack traces, and stop the watcher to verify that no child remains and no port stays occupied.