Sub topic 2
What a diagnostic is made of
This chapter assumes you can run npx tsc in a project with Node.js and npm. It also assumes you know basic type annotations, interfaces, and union types. Here you will learn the parts of one compiler message. Later chapters use these parts to explain why the compiler complains.
One error, four questions
Every message from the compiler answers four questions:
- Where is the problem? This is the file, line, and column.
- How serious is it? This is the category, such as error or warning.
- Which rule was broken? This is the numeric code, such as
TS2345. - What does the compiler say is wrong? This is the message text.
Here is a small program with a mistake.
function double(n: number): number {
return n * 2;
}
const input: string = "21";
double(input);Save it as src/index.ts and run the compiler in check-only mode.
$ 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 --noEmit flag makes the compiler check types and write no .js files. The --pretty false flag turns off colors and code frames, so the output is one line per diagnostic. That form is easy to search with grep and easy to read in CI logs.
The location
In the plain format, the location is file(line,column).
src/index.tsis the path relative to where you rantsc.6is the line number. Lines start at 1.8is the column number. Columns also start at 1.
In the example, line 6 is double(input);. The text double( takes seven characters, so input starts at column 8. The compiler points at the argument, not at the function name. The location marks the start of the exact piece of code the compiler blames. That piece is often not the line that caused the problem.
The compiler tracks an end position too, but plain output prints only the start. The pretty format and your editor use the end position to underline the whole expression.
The pretty format
When you run tsc in a terminal without --pretty false, the compiler colors its output and shows a code frame. The location also changes shape to file:line:column, which many terminals let you click.
src/index.ts:6:8 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
6 double(input);
~~~~~
Found 1 error in src/index.ts:6The squiggle shows the range the compiler blames. The last line summarizes the count and the first location per file.
The pretty format also prints related information. This is a second location that helps explain the error. Consider this code.
interface User {
id: number;
name: string;
}
const user: User = { id: 1 };The compiler reports TS2741 on the variable and points back to the missing property.
src/user.ts:6:7 - error TS2741: Property 'name' is missing in type '{ id: number; }' but required in type 'User'.
6 const user: User = { id: 1 };
~~~~
src/user.ts:3:3
3 name: string;
~~~~
'name' is declared here.The second block is not another error. It is a pointer to a second place worth reading. The plain --pretty false output omits it, so switch to pretty output when a one-line message leaves you unsure.
The category
Between the location and the code sits the category word. These are the categories you will see:
| Category | Meaning | Where it appears |
|---|---|---|
| `error` | The program is invalid under your settings. | `tsc` output, editor red underline |
| `warning` | A problem that does not fail the check. | Rare in `tsc` |
| `suggestion` | An optional improvement. | Editors only |
| `message` | Plain information. | Related information, `--listFiles` style output |
For command-line use, only error matters. Any error makes tsc exit with a non-zero status, and that is what stops a CI job. A suggestion, such as an editor hint that a file could use async, never fails a build.
The error code
The code has the form TS followed by a number, such as TS2345. The number is stable, so it survives rewording of the message between compiler versions. The message text can change from one release to the next, but TS2345 keeps its meaning.
The leading digits hint at the area of the compiler that produced the error.
| Range | Typical area | Example |
|---|---|---|
| 1xxx | Syntax and parsing | `TS1005: ';' expected.` |
| 2xxx | Type checking | `TS2322`, `TS2345`, `TS2339`, `TS2741`, `TS2307` |
| 5xxx | Compiler options and config | Invalid `tsconfig.json` values |
| 7xxx | Strictness-related checks | `TS7006` implicit `any` |
| 18xxx | Later additions to the checker | `TS18048: 'x' is possibly 'undefined'.` |
Treat the ranges as a rough guide, not a contract. A 1xxx error means the compiler could not even parse your code, so fix those first. Type-checking errors in the same file may be a side effect of the parse failure. The chapter on triage returns to this point.
The code is also your best search term, because it is shorter and more exact than the message. The next chapter covers how to use it.
The message text
The message is a sentence written from templates. The compiler fills in names and types between single quotes. In Argument of type 'string' is not assignable to parameter of type 'number'., the template has two holes: the type you gave and the type that was expected.
Learn to scan for these quoted parts. They tell you what the compiler believes about your code, and that is often different from what you believe. If you thought input was a number, the message corrects you: the compiler thinks it is string.
Some messages continue on indented lines. This is a message chain, where each line explains the one above it.
type Config = { retries: number; mode: "fast" | "safe" };
function run(config: Config): void {
console.log(config.retries, config.mode);
}
const config = { retries: 3, mode: "fast" };
run(config);src/config.ts(7,5): error TS2345: Argument of type '{ retries: number; mode: string; }' is not assignable to parameter of type 'Config'.
Types of property 'mode' are incompatible.
Type 'string' is not assignable to type '"fast" | "safe"'.The first line is the outermost claim. The last line is the root cause. The location refers to the whole diagnostic, not to each line. The chapter on message chains covers reading these in detail.
Long types are truncated
For very large types, the compiler may shorten the text and replace the middle with .... If you need the full text, run:
npx tsc --noEmit --noErrorTruncationYou can also hover over the symbol in your editor, which shows the type in a popup.
The summary and the exit status
The compiler prints diagnostics in the order it finds them, grouped by file. This is not always the order of the lines in your source. The pretty format ends with a Found N errors line. In scripts, rely on the exit status instead of parsing that line. The status is 0 when there are no errors, and non-zero otherwise.
npx tsc --noEmit --pretty false && echo "types ok" || echo "type errors"The editor shows the same data
Your editor uses the same checker through the TypeScript language service. The red underline is the range, hovering shows category, code, and message, and the Problems panel lists the file:line:column location. A diagnostic seen in the editor and one seen in tsc should match. If they do not, the usual cause is that the editor is using a different TypeScript version or a different tsconfig.json. Check which version your editor runs, and compare it with npx tsc --version.
Practice
Exercise
Given this line of output, name the file, line, column, category, error code, and the two types the message mentions: src/app.ts(12,15): error TS2322: Type 'number' is not assignable to type 'string'.
Show answer
File src/app.ts, line 12, column 15, category error, code TS2322. The message mentions the assigned type number and the target type string. The value on line 12 starting at column 15 is a number being assigned to something declared as a string.
Exercise
A teammate pastes a diagnostic with the location src/user.ts:6:7 and a second block pointing to src/user.ts:3:3 with the text "'name' is declared here." Is the second block a second error? What should you do with it?
Show answer
No. It is related information, a pointer to a second place that helps explain the error. Open that location to see the declaration involved, here the name property required by the type. Fix the error at the first location.
Exercise
Why is the error code a better search term than the message text?
Show answer
The code is short, exact, and stable across compiler versions, while the message contains names from your own code and its wording can change between releases.