Zum Inhalt springen
KodeTrail

Camp 1 · Schritt 3 von 13

Reading type errors

Decode TypeScript's error messages — they're wordy, but they follow one simple pattern.

12 Min.+50 XPAuf Englisch angezeigt — Übersetzung ist unterwegs

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

TypeScriptCloud-Run
Error saysIt 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 1a 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'?".

Checkpoint

TypeScript says: Type 'boolean' is not assignable to type 'string'. What happened?

Checkpoint

Property 'lenght' does not exist on type 'string'. Did you mean 'length'? — best response?