Skip to lesson

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.

Beginner to advanced·13 min·Getting started·Updated 2026-11-24

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:

typescript
const age: number = "thirty";
result
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 name age, where the problem starts.
  • error is the severity. The compiler also has warnings in some tools, but tsc reports problems as errors.
  • TS2322 is the error code. Every kind of problem has a number. If a message puzzles you, searching for TS2322 finds 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:

typescript
function square(n: number): number {
  return n * n;
}

square("4");
result
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:

typescript
const book = { title: "Dune", pages: 412 };
console.log(book.author);
result
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:

typescript
function shout(word: string, suffix?: string) {
  return word.toUpperCase() + suffix.toUpperCase();
}
result
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:

typescript
function double(n) {
  return n * 2;
}
result
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:

typescript
type Order = { id: number; customer: { name: string; email: string } };

const draft = { id: 1, customer: { name: "Asha", email: 42 } };
const order: Order = draft;
result
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

  1. Read the first line, then the last line of a long error.
  2. Go to the line and column in your editor. The underlined part is what the checker disagrees with.
  3. 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.
  4. Fix one error at a time, then recompile. One mistake can cause several errors, and fixing the first often clears the rest.
  5. Do not silence an error you do not understand. Later lessons cover any and 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.

Next: Type annotations and type inference.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 In example.ts(3,7): error TS2322, what do the numbers 3 and 7 mean?
02 Which error code means that an argument does not match the parameter's type?
03 A long multi-line error is easiest to understand by reading
04 What does error TS7006 tell you in strict mode?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Frontend

TypeScript tutorial

Add types to JavaScript to catch mistakes before your code runs, from your first annotation to generics and real projects.

Your progress 0/42
Learning something new?Explore all courses