TypeScript Reference ยท The tsc Command Line
tsc exit codes
Reference for tsc exit codes: 0 for success, 1 and 2 for type errors with or without output, 3 and 4 for project problems, and how to use them in scripts.
When tsc finishes it returns a numeric exit status to the shell. Scripts, npm and CI systems use that number to decide whether a step passed. Knowing the values explains why a build can both "emit files" and "fail".
tsc && echo "types are fine" tsc --noEmit; echo $?
Try it
A program is judged by whether the checker found problems, not by whether it would run. The first example below has a type error (the run stops, like a non-zero exit); the second is clean.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
| Code | Name | Meaning |
|---|---|---|
| 0 | Success | No errors. |
| 1 | DiagnosticsPresent_OutputsSkipped | Errors were found and no files were emitted (for example with --noEmit or noEmitOnError). |
| 2 | DiagnosticsPresent_OutputsGenerated | Errors were found, but output files were still written. |
| 3 | InvalidProject_OutputsSkipped | --build found an invalid project configuration. |
| 4 | ProjectReferenceCycle_OutputsSkipped | --build found a cycle in project references. |
Notes: codes 3 and 4 appear only in build mode. Command-line misuse (such as combining -p with file names) is reported as a diagnostic and gives a non-zero code too; test != 0, not a specific number, unless you need to tell the cases apart.
Usage notes
- Common case: a plain
tscrun with type errors andnoEmitOnErroroff returns 2, because JavaScript was still emitted. With--noEmitthe same errors give 1. - npm scripts stop at any non-zero code, so
"build": "tsc && node dist/main.js"never runs the second command after type errors. - Do not rely on "no output". Always check the status; some warnings and messages go to standard output only.
- Watch mode keeps running after errors; there is no exit status to check until it is stopped.
- Node tools can set the status with
process.exitCode, as in the example. Prefer it toprocess.exit()so pending output is flushed. - CI tip: use
tsc --noEmitas a dedicated step, so a failure is clearly a type error and not a bundler problem.
Related
- tsc
- tsc --noEmit
- noEmitOnError
- Tutorial: Tooling