← Trail· Lesson 2 of 10
See this lesson in JavaScript →
Variables and types
Use let and const with type annotations, inference and the number | string | boolean union.

A variable gives a value a name. Use const when you will not reassign it and let when it changes.
| Type | Example |
|---|---|
number |
let idade: number = 19 |
string |
const nome: string = 'Ana' |
boolean |
const estuda = true |
You don’t need to annotate everything. In const estuda = true, TypeScript infers boolean. After let idade: number, assigning idade = '20' is a type error. At runtime, typeof still reports the actual type.
number | string | boolean is a union: the value may be any of those types.
Try it
Change the values and compare the Console lines. estuda has no annotation but still has a type.
Execute o código para ver a saída aqui.Exercise
tipoDoValor(valor: number | string | boolean): string always returns 'number'. Replace the fixed value of tipo with typeof valor and click Verificar. Do not put quotes around it. Feedback is in Portuguese.
Execute o código para ver a saída aqui.Edite o TypeScript e clique em Verificar.
Next step
Predict the types of false, 'false' and 0. TypeScript checks types in the editor; typeof discovers them while the program runs.