Reading type errors
You'll learn to read a TypeScript error, find what you gave and what was expected, and fix the code instead of hiding the error.
Watch & read
Start with the required ones. The others are there when you need them.
Explanation
A red line appears in VS Code. You hover over it and get a long sentence full of quotes. It looks scary, but most TypeScript errors say the same simple thing: you gave one type, but another was expected.
Think of a phone charger. You try to plug a Type-C cable into a Lightning port. It doesn't fit, and you can see which plug you have and which socket it needs. A type error is that mismatch, written as text.
You can see an error in three places: the red underline (hover it), the Problems panel in VS Code, or the terminal, where it looks like this:
app.ts(4,7): error TS2322: Type 'string' is not assignable to type 'number'.
File app.ts, line 4, character 7. TS2322 is the error's number; search it if the words don't help.
Read "is not assignable to" as "doesn't fit into". The first type is what you gave. The second is what
was expected.
The messages you'll see most:
Type 'X' is not assignable to type 'Y'.A value of the wrong type.Argument of type 'X' is not assignable to parameter of type 'Y'.The same, but in a function call.Property 'x' does not exist on type 'Y'.A spelling mistake, or a field the object doesn't have.Property 'x' is missing in type 'A' but required in type 'B'.You forgot a field.'x' is possibly 'undefined'.Check forundefinedfirst.Expected 2 arguments, but got 1.A function call with too few values.
Some errors take several lines. Read the first line for where, and the last line for why.
Type '{ name: string; year: string; }' is not assignable to type 'Student'.Types of property 'year' are incompatible.Type 'string' is not assignable to type 'number'.
The last line is the real reason: year is a string, but Student needs a number.
People often, and AI very often, make the red line go away without fixing anything: as any, a !, or a
// @ts-ignore comment above the line. These tell TypeScript to stop checking. The bug is still there, and
now nobody warns you. Fix the value or the check instead. Change the type only when the type itself is
wrong.
When you ask AI for help, paste the full error, the line it points to, and the type it mentions (like the
type Student = ... lines). Then ask: "What did I give, and what was expected?"
Code tour
Click a line number with a ring to read the note for that line.
Practice
Short drills. Take your time and read the code before you answer.
1type Product = { title: string; price: number };2const saved = { title: "Tea", price: "20" };3const tea: Product = saved;45// app.ts(3,7): error TS2322: Type '{ title: string; price: string; }' is not assignable to type 'Product'.6// Types of property 'price' are incompatible.7// Type 'string' is not assignable to type 'number'.
1function addDelivery(price: number, delivery: number) {2 return price + delivery;3}4addDelivery(200);
Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3
Type 'string' is not assignable to type 'number'.What happened? - 2 / 3A TypeScript error has three lines. Which line tells you the real reason?
- 3 / 3AI adds
as anyand the red line disappears. What is true now?