TypeScript in Practice

Getting started · Lesson 3 of 12 · 14 min

Reading error messages

TypeScript errors look scary because they're long. They're long because they explain the path to the problem. Read them from the bottom up.

Argument of type '{ id: number; name: string; }' is not assignable to parameter of type 'User'.
  Property 'email' is missing in type '{ id: number; name: string; }' but required in type 'User'.

The last line is the real problem: email is missing. The first line only tells you where it happened.

The three questions

  1. What did I give? The type on the left of "is not assignable".
  2. What was expected? The type on the right.
  3. What's the difference? Usually named on the last line.
type User = { id: number; name: string; email: string };
 
function save(user: User) {
  /* … */
}
 
save({ id: 1, name: "Maya" }); // ❌ email is missing

Hover the red squiggle and press Ctrl + . in VS Code. "Add missing properties" fixes this one for you.

Exercise: open exercises/errors.ts in the starter project and fix the five errors without using any or as.