Sub topic 1
Running the compiler and finding your errors
Prerequisites
This chapter assumes you can write basic JavaScript (functions, objects, arrays, modules) and that you know TypeScript annotations, interfaces, and union types. You need Node.js and npm installed and a terminal you can run commands in. You don't need to have fixed many compiler errors before. This chapter covers how to run the compiler and find the errors it reports. Later chapters cover what the errors mean.
Setting up a project the compiler can check
The TypeScript compiler is a program called tsc. Install it as a development dependency of the project, so every developer and the CI server use the same version:
mkdir ts-errors && cd ts-errors
npm init -y
npm install --save-dev typescript
npx tsc --versionnpx tsc runs the copy in node_modules/.bin. A globally installed tsc can be a different version and report different errors, so prefer the local one.
Next, add a tsconfig.json at the project root. This file tells the compiler which files to check and which rules to apply.
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"noEmit": true
},
"include": ["src"]
}Three of these settings matter most here:
includelists the files to check. Only files undersrcare part of the program.strictturns on a family of stricter checks. A later chapter in this tutorial covers it. For now, know that it changes which errors you see.noEmittells the compiler to check the code and write no JavaScript files.
Create src/index.ts with a deliberate mistake:
function double(n: number): number {
return n * 2;
}
const input: string = "21";
double(input);Running the compiler
From the project root, run:
npx tsc --noEmit --pretty falseThe output looks like this:
$ npx tsc --noEmit --pretty false
src/index.ts(6,8): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.The error is on line 6, column 8 of src/index.ts. That is the input argument in double(input). The --noEmit flag matches the config file, so passing it on the command line is optional here. The --pretty false flag switches off colors and the code excerpt, which makes the output one plain line per error. That form is easy to search, copy into a ticket, or parse in a script.
Without --pretty false, a terminal shows a colored view with the source line underlined and a summary such as Found 1 error in src/index.ts. Both forms hold the same information. Use the pretty form while you read errors and the plain form when a program consumes them.
The compiler reads tsconfig.json only when you give it no files
A common surprise is this command:
npx tsc src/index.tsWhen you name files on the command line, tsc ignores tsconfig.json completely. The file is compiled with default options, which means no strict, and the output is different. You may see fewer errors than your project reports, or different ones. Run plain npx tsc (or npx tsc --noEmit) to use the config. If you want a different config file, pass it with -p:
npx tsc -p tsconfig.json --noEmitIf an error you expect does not appear, or an unexpected one does, check which files and options the compiler is using:
npx tsc --showConfig
npx tsc --listFilesOnly--showConfig prints the final configuration after defaults are filled in. --listFilesOnly lists every file in the program, including type definitions from node_modules. If your file isn't listed, the include pattern does not match it, and the compiler is not checking it at all. A project with a misplaced include can report zero errors while being full of them.
What the compiler does when there are errors
The compiler goes through a fixed sequence for each run, shown below.
Two behaviors surprise people:
- Errors do not stop emit by default. Unless you set
noEmitOnError,tscwrites JavaScript even when type errors exist. The type checker reports problems, and the emit step is separate. A build can "succeed" in producing files while the command still fails. - The exit code reports the result.
tscexits with0when it finds no errors and a non-zero code when it does. Scripts and CI rely on this, so don't ignore it by pipingtscoutput through something that hides the status.
Check the exit code yourself in a POSIX shell:
npx tsc --noEmit
echo "exit code: $?"Many projects use a bundler or a tool like tsx to run TypeScript and never call tsc to build. Those tools often strip types without checking them. In that setup tsc --noEmit is the only step that reports type errors, so it must still run somewhere.
Running a check from package.json
Add a script so everyone runs the same command:
{
"scripts": {
"typecheck": "tsc --noEmit"
}
}Run it with npm run typecheck. When a check fails, npm prints its own extra lines about the failed script. The lines that start with a file path are the compiler's diagnostics.
Watch mode
For a fix-and-recheck loop, use watch mode:
npx tsc --noEmit --watchThe compiler checks the whole project once, then rechecks incrementally whenever a file is saved. It clears the screen and prints the error count, for example Found 0 errors. Watching for file changes. Keep this open in a second terminal while you edit.
Editor diagnostics versus command-line diagnostics
Your editor shows red underlines because it runs the TypeScript language service, which is built on the same checker as tsc. Hovering over an underline shows the same message and code. Two differences are worth knowing:
- The editor may use its own bundled TypeScript version instead of the one in
node_modules. In VS Code you can pick the workspace version from the command palette. If the editor andtscdisagree, a version mismatch is the first thing to check. - The editor checks only open files and files they import, in real time.
tscchecks the whole project. An error in a file you have not opened shows up only in the command-line run or the Problems panel with a project-wide check.
Treat the command-line result as the one that counts. CI runs it, and it covers every included file.
Reading the list of errors
With many errors, tsc prints them in the order it checks files, grouped by file. A few habits make the list easier to use:
- Count first. The summary line tells you whether you face three errors or three hundred.
- Look at the file names. Many errors in one file often share one cause.
- Read from the top, not the bottom. Later errors may result from earlier ones. A later chapter covers this in detail.
- Use the error code, such as
TS2345, as a stable label. The message text can change between versions, but the code stays the same.
Common problems when running the compiler
| Symptom | Likely cause | What to check |
|---|---|---|
| `tsc: command not found` | TypeScript is not installed, or you used `tsc` without `npx` | Run `npm install --save-dev typescript`, then `npx tsc` |
| No output, but you expect errors | The file is not in `include`, or the output is hidden | Run `npx tsc --listFilesOnly` |
| `error TS18003: No inputs were found in config file` | `include` matches no `.ts` files | Check the folder name and path in `tsconfig.json` |
| Different errors in editor and terminal | Different TypeScript versions or options | Compare `npx tsc --version` with the editor's version |
| Errors appear but JavaScript is still written | `noEmitOnError` is off and `noEmit` is not set | Set `noEmit` for checks or `noEmitOnError` for builds |
Exercise
You run npx tsc src/app.ts and see no errors, but npx tsc --noEmit reports a TS18048 error in the same file. Give one likely reason, and name a command that shows which options the project actually uses.
Show answer
Naming a file on the command line makes tsc ignore tsconfig.json, so strict (and strictNullChecks, which produces TS18048) was off in the first run. Running npx tsc --noEmit uses the config, so the error appears. npx tsc --showConfig prints the options the project uses.
Exercise
A diagnostic reads src/index.ts(6,8): error TS2345: .... State the file, the line, the column, and the error code.
Show answer
The file is src/index.ts, the line is 6, the column is 8, and the error code is TS2345.