AI Hints
Home›TypeScript Tutorial›First Steps
TypeScript · Lesson 5 of 120

Reading Type Errors

What tsc tells you.

⏱ 2 min read▶ Video at 4:07🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Type errors look scary at first, but they follow a simple pattern. The file, the line, and a sentence explaining what didn't fit.

Reading Type Errors
From the video: Reading Type Errors
  • File
  • Line
  • What didn't fit

A wrong assignment

Bikes is a number. On line two, we try to put text into it. Run it. The error says line two, and that a string isn't assignable to a number.

app.tstypescript
let bikes: number = 12;
bikes = "twelve";
console.log(bikes);
Output
app.ts line 2: Type 'string' is not assignable to type 'number'.

A misspelled name

Here we misspell the variable name. Run it. TypeScript can't find it, and even suggests the name we probably meant.

app.tstypescript
const stockCount: number = 12;
console.log(stokCount);
Output
app.ts line 2: Cannot find name 'stokCount'. Did you mean 'stockCount'?

Reading any error

Start with the line number. Read the sentence slowly, it usually says this type isn't assignable to that type. Look at both types. And fix the value, or the type, whichever is wrong.

keyvalue
Firstthe line number
Thenthe sentence
Comparethe two types
Fixvalue or type

Summary

Every type error gives a line, and a sentence about what didn't fit. Next, type inference.

Key takeaways
  • Line number
  • Assignable
  • Did you mean
Next up: Type Inference — TypeScript works it out