lecture 5 of 120 completed

Reading a type error

A long type error has a fixed structure. The last line names the real mismatch, and the fix is often on a different line from the error.

What you'll learn

  • The four parts of every compiler error
  • Why you read a long error from the bottom
  • How to decide where the fix belongs

A test saves a loan. The compiler answers with this.

interface Member {
  name: string;
  card: { id: number; active: boolean };
}

interface Loan {
  book: string;
  member: Member;
}

function saveLoan(loan: Loan): void {}

const loan = {
  book: "Dune",
  member: { name: "Ana", card: { id: "A-17", active: true } },
};

saveLoan(loan);
loan.test.ts(18,10): error TS2345: Argument of type '{ book: string; member: { name: string; card: { id: string; active: boolean; }; }; }' is not assignable to parameter of type 'Loan'.
  The types of 'member.card.id' are incompatible between these types.
    Type 'string' is not assignable to type 'number'.

The first line of that error is long, and most people stop reading inside it. Then they look for a way to silence it. The error is not difficult. It has a structure, and the useful part is at the end.

The four parts

Every compiler error has the same parts, in the same order.

  1. Where. loan.test.ts(18,10) is the file, then line 18 and column 10.
  2. The code. TS2345 is a number for this kind of error. You can search for it.
  3. The first sentence. It says what you gave and what was expected. Here the whole object is printed, which is why the line is so long.
  4. The indented lines. Each one goes one level deeper into the value. The last one is the real mismatch.

Read from the bottom

Start with the last line.

Type 'string' is not assignable to type 'number'. Somewhere a text value sits where a number belongs.

Then read the line above it. The types of 'member.card.id' are incompatible between these types. That is the path to the field: inside member, inside card, the field id.

Now you know everything. The card id is "A-17", which is text, and Member says a card id is a number. You did not need the first sentence at all.

The error line is not always the fix line

The compiler reported line 18, the call to saveLoan. Nothing is wrong on line 18. That is only where the object and the type first met.

The fix is in one of two other places, and only you can choose.

  • The data is wrong. Card ids are numbers, and the test data has a typing mistake. Fix line 15.
  • The type is wrong. Card ids in Shelf really look like "A-17". Then Member is out of date, and the fix is id: string on line 3.

Check the application or its API before you choose. Changing whichever line is closer gives you a clean compile and a test that describes an application that does not exist.

Codes you will meet in this track

Code What it means
TS2322 A value does not fit the variable or field it is put into
TS2345 An argument does not fit the parameter
TS2339 You read a field that the type does not have
TS2554 A call has the wrong number of arguments
TS7006 A parameter has no type, so it became any
TS18048 A value may be undefined where you use it

The codes are steadier than the sentences. A newer version of TypeScript may word a message differently, so search for the code.

One mistake, several errors

One wrong line can produce errors in several places. A wrong field in an interface is reported at every object that uses it. Fix the first error, run the check again, and read what is left. Often the rest are gone.

Some messages end with a suggestion.

const loan = { book: "Dune", daysLate: 3 };
console.log(loan.dayslate);
loan.test.ts(2,18): error TS2551: Property 'dayslate' does not exist on type '{ book: string; daysLate: number; }'. Did you mean 'daysLate'?

The suggestion is a guess from the names the compiler knows. It is usually right for a typing mistake.

Do not silence what you have not read

TypeScript has ways to switch an error off, and lecture 12 covers them. A reviewer will ask why each one is there. "The message was long" is the usual honest answer, and it is not a good one. An error you silence stays in the code. The red line goes, and the test data is still wrong.

Remember this

A compiler error gives the place, a code, a first sentence and then indented detail. Read the last line first, then the path above it. The line the compiler names is where the mismatch was noticed. Decide whether the data or the type is wrong before you change either.

Reference: Understanding Errors in the TypeScript Handbook.

now prove it

Reading is the setup. Solve this exercise in the editor. We check your code with the TypeScript compiler and list each type error.