Skip to content
Theory · First steps · lesson 4 of 7

Compile errors

When an inspector finds a violation, they write a report. It says which parcel it is, where the violation was found, which rule is broken and what is wrong. tsc reports an error in the same detail, and now we'll learn to read its report.

An error that tsc finds while checking is called a compile error: it shows up before the run, from the program's text. Here is a file hello.ts where a price was given text:

TypeScript
let price: number = 100;
price = "free"; // ✗

The variable price is declared with the type number, and the second line puts the string "free" into it. The command tsc hello.ts prints in the terminal:

TEXT
hello.ts:2:1 - error TS2322: Type 'string' is not assignable to type 'number'.

2 price = "free";
  ~~~~~


Found 1 error in hello.ts:2

The first line, part by part

The main part of the message is its first line. Let's take it from left to right:

  1. hello.ts: the file with the error.
  2. 2: the line number.
  3. 1: the column number, that is, the character of the line where the problem starts. Here it is the first character, the name price.
  4. error: the label that marks an error.
  5. TS2322: the error code.
  6. Type 'string' is not assignable to type 'number'.: the text, "a string cannot be assigned to a number".

Such a text names the type of the value being put first, then the type of the place it goes into. Here a string is being put into a variable for numbers.

The underline and the summary

Below the first line tsc repeats the line of code with its number and underlines the problem with a wave, ~~~~~. The last line sums up how many errors were found and in which file.

The short form

When the output of tsc goes not to the screen but to a file or another program, the same message looks shorter:

TEXT
hello.ts(2,1): error TS2322: Type 'string' is not assignable to type 'number'.

The parts are the same, only the line and column stand in parentheses, and there is no underline. In the course lessons a message keeps its code and text: error TS2322: Type 'string' is not assignable to type 'number'.

The error code

tsc writes its messages in English. A flag is an addition to a command that changes what it does: tsc --locale ru hello.ts prints the messages in Russian. The error code stays the same in any language. Searching for the code TS2322 finds explanations and examples, so the course quotes messages as they are and explains what they mean in words.

The program did not run

Notice that tsc found the error without running a single line. It reads the text, so it sees errors even in code that never runs. Nobody calls this function, and it still has an error:

TypeScript
function neverCalled(): number {
    return "ten"; // ✗
}
TEXT
error TS2322: Type 'string' is not assignable to type 'number'.

The function promised to return a number, and it returns a string. JavaScript would never learn about such an error: the run never reaches this line.

Common mistake

Fixing what the message names when the cause is on another line:

TypeScript
function double(n: number): number {
    return n * 2;
}

const input: string = "21";
console.log(double(input)); // ✗
TEXT
error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

The message points to line 6: "an argument of type string cannot be passed to a parameter of type number". It names a parameter of type number. If you "fix" that parameter and write n: string, the error moves to the second line: a string cannot be multiplied by 2. The real cause is on line 5: input holds text where a number is needed. The fix is const input: number = 21;.

The place in the message shows where tsc noticed the mismatch. The mistake itself may sit higher up.

Key points

  • tsc finds a compile error from the program's text without running anything, even in code that never runs.
  • The first line of a message holds the file, line, column, code and text: hello.ts:2:1 - error TS2322: ….
  • A code like TS2322 is the same in any language, and you search by it.
  • The message shows where tsc noticed the mismatch; the cause may be a line higher.