Understanding type compatibility

In this lesson, we'll learn how TypeScript decides whether an item can be assigned to another, i.e. how TypeScript decides whether types are compatible.

Basic type compatibility #

Consider the code below. Hopefully, it is no surprise that TypeScript isn’t happy with the assignment on the last line because the types aren’t compatible.

let firstName: string = "Fred";
let age: number = 30;
firstName = age;  // Type 'number' is not assignable to type 'string'.

What about the code below. Will TypeScript be happy with the assignment on the last line?

Get hands-on with 1400+ tech skills courses.