TypeScript · Lesson 4 of 42
Reading TypeScript compiler errors
Learn the parts of a TypeScript error message, how to read long multi-line errors from the bottom up, and fix the most common error codes.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will stop being afraid of red text. A TypeScript error is a structured sentence that tells you where, what and usually why. Learning to read it quickly is the single most useful skill in this course, because you will see hundreds of them.
The anatomy of an error
Here is a complete error from the compiler. The sample prints it exactly as tsc shows it:
const age: number = "thirty";example.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.Break it into four parts:
example.ts(1,7)is the file, then the line and column in brackets. Line 1, column 7 is the nameage, where the problem starts.erroris the severity. The compiler also has warnings in some tools, buttscreports problems as errors.TS2322is the error code. Every kind of problem has a number. If a message puzzles you, searching forTS2322finds explanations.- The message says what is wrong in words: a string cannot go where a number is expected.
The five errors you will meet most
1. Type is not assignable (TS2322). A value does not fit the place it is going, as above. The fix is usually to correct the value or to correct the type if your assumption was wrong.
2. Argument is not assignable to a parameter (TS2345). The same idea, for a function call:
function square(n: number): number {
return n * n;
}
square("4");example.ts(5,8): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.3. Property does not exist (TS2339). You used a property the type does not have, often a typo or a property that exists only on some members of a union:
const book = { title: "Dune", pages: 412 };
console.log(book.author);example.ts(2,18): error TS2339: Property 'author' does not exist on type '{ title: string; pages: number; }'.4. Possibly undefined (TS18048 and relatives). In strict mode the checker knows a value might be missing, and it makes you handle that:
function shout(word: string, suffix?: string) {
return word.toUpperCase() + suffix.toUpperCase();
}example.ts(2,31): error TS18048: 'suffix' is possibly 'undefined'.5. Implicit any (TS7006). A parameter has no annotation and the compiler cannot work out its type:
function double(n) {
return n * 2;
}example.ts(1,17): error TS7006: Parameter 'n' implicitly has an 'any' type.The fix is to say what n is: function double(n: number).
Reading long errors from the bottom
Some errors are several lines long, with indented lines under the first. They unpack a nested mismatch. Here is one:
type Order = { id: number; customer: { name: string; email: string } };
const draft = { id: 1, customer: { name: "Asha", email: 42 } };
const order: Order = draft;example.ts(4,7): error TS2322: Type '{ id: number; customer: { name: string; email: number; }; }' is not assignable to type 'Order'.
The types of 'customer.email' are incompatible between these types.
Type 'number' is not assignable to type 'string'.The first line is the headline: the whole draft object does not fit Order. The indented lines below it go deeper. The second line narrows it to one property, customer.email, and the last line gives the real cause: a number where a string is needed. Read the last indented line first. It names the deepest mismatch, which is almost always the thing to fix. Then read upward to see how that small mismatch makes the bigger value fail.
A routine for fixing errors
- Read the first line, then the last line of a long error.
- Go to the line and column in your editor. The underlined part is what the checker disagrees with.
- Ask which side is wrong: the value or the type you wrote. Often the type is right and the value is a mistake. Sometimes your type was too narrow.
- Fix one error at a time, then recompile. One mistake can cause several errors, and fixing the first often clears the rest.
- Do not silence an error you do not understand. Later lessons cover
anyand type assertions, and explain why they should come last.
Common mistakes
Fixing the last error first. Errors are listed in order. An early mistake, such as a wrong type on a variable, can cause many later errors. Start at the top.
Treating the message as an insult. The compiler is describing a real mismatch. Read it literally: "Type string is not assignable to type number" means exactly that.
Silencing errors with any. It makes the red line disappear and the bug stay. Use it only when you have a reason.
Ignoring the code. Searching for the TS code, or asking an editor to explain it, is faster than guessing.
Exercise
Create a file with the five samples above and compile it. Match each error to its code in this lesson. Then fix each one with the smallest change you can: change a value, add a check, or add a type annotation. For the possibly undefined error, try two fixes, a check with if and the optional chaining operator ?., and notice how the return type changes.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.