Camp 1 · Step 3 of 13
Reading type errors
Decode TypeScript's error messages — they're wordy, but they follow one simple pattern.
TypeScript errors look intimidating — long, formal, full of quotes. But almost every one follows the same sentence shape. Crack the shape, read them all.
The universal pattern
Type 'A' is not assignable to type 'B'.
Translation: "You put an A where I was promised a B." That's it. The
error names what you gave (A), what was expected (B), and points at
the exact location.
let lessons: number = "thirteen";
// Type 'string' is not assignable to type 'number'.You gave a string; a number was promised. Fix either side: change the value, or (if the type was wrong) change the annotation.
A tour of the greatest hits
| Error says | It usually means |
|---|---|
Property 'nmae' does not exist on type… | typo in a property name — and it lists what does exist |
'string' is not assignable to 'number' | wrong value type at an assignment or argument |
Expected 2 arguments, but got 1 | a call is missing something |
Object is possibly 'undefined' | you must check a value exists before using it |
Notice the compiler often names the fix: misspell name as nmae and
it asks "Did you mean 'name'?".
TypeScript says: Type 'boolean' is not assignable to type 'string'. What happened?
Property 'lenght' does not exist on type 'string'. Did you mean 'length'? — best response?