RivoCode

TypeScript in Depth · Type Basics · Lesson 1 of 16

Adding Types to Variables

Concept

Adding Types to Variables

TypeScript lets you annotate variables with a type. The compiler then catches mismatched values before you run the code.

By the end of this lesson
  • Explain the core idea behind Adding Types to Variables.
  • Predict output before running a change.
  • Test one realistic and one unusual input.
  • Use the result to make the next decision.
How to study this page
  1. 1. Read one concept.
  2. 2. Change the example.
  3. 3. Run, compare, and explain.
  4. 4. Complete the challenge below.
ExampleRunnable
let age: number = 20;
let name: string = "Alex";
Try it Yourself »
Self-check before continuing

Without looking at the example, describe what changes when you modify one input in Adding Types to Variables. Then reopen the editor and prove your explanation with a small test.

You are ready to continue when you can predict, test, and explain the result.

Your turn

Declare a boolean variable with an explicit type and assign it a value.

Loading editor…

Console

Output will appear here...