...

/

Using type annotations

Using type annotations

In this lesson, we'll learn how to assign types to variables and functions using type annotations.

Assigning types to variables #

TypeScript’s type annotations allow us to assign types to variables. The syntax for a type annotation is as follows: put a colon followed by the variable name and type before any assignment. For example, the score variable is declared as a number in the code below:

let score: number;

Let’s assign a couple of different values to score:

let score: number;
score = 10;
score = "ten";

If you run the code above, a type error is raised on line 3. Why is this?

...