typescript-variables

Variables in TypeScript

  • 3 min

In TypeScript, we can declare variables using the let or const keyword, followed by the variable name (optionally, its type).

The let keyword declares variables that have block scope. This means the variable is only accessible within the block where we define it.

let name: string = "Luis";
name = "Carlos"; // Correct
console.log(name); // Carlos
Copied!

The const keyword is used to declare constants (i.e., variables whose value will not change once assigned). They also have block scope.

const PI: number = 3.1416;
console.log(PI); // 3.1416

// PI = 3.14; // Error: Cannot assign a new value to a constant
Copied!

Variable Scope

The scope of a variable determines where it can be accessed in the code. TypeScript has three main types of scope.

Global Scope

A variable declared outside of any function or block has global scope and is accessible from anywhere in the code.

let globalVar: string = "I am global";

function showGlobal() {
    console.log(globalVar); // I am global
}
Copied!

Block Scope

Variables declared with let or const inside a block (for example, within an if, for structure, or {}) are only accessible within that block.

if (true) {
    let blockLet: string = "Inside the block";
    console.log(blockLet); // Inside the block
}
// console.log(blockLet); // Error: blockLet is not defined
Copied!

Function Scope

A variable declared inside a function is only accessible within that function. Variables declared with var have function scope.

function exampleFunction() {
    var functionVar: string = "Inside the function";
    console.log(functionVar); // Inside the function
}
// console.log(functionVar); // Error: functionVar is not defined
Copied!

Using var is not recommended nowadays. In fact, I haven’t even listed it as an option at the beginning. Seriously, don’t use it.

Variable Types in TypeScript

In TypeScript, we can assign different types to our variables. For example, like this,

let name: string = "Luis";
let age: number = 25;
let isAdult: boolean = true;
let numberList: number[] = [1, 2, 3, 4, 5];
let person: { name: string, age: number } = { name: "Luis", age: 25 };
Copied!

In the rest of the course, we will delve deeper into the different available types and how to use them, as they are the main appeal of TypeScript.

Type Inference

In TypeScript, the compiler can automatically infer the type of a variable based on the value we assign to it.

This means it is not always necessary to explicitly specify a variable’s type, as TypeScript can deduce it on its own.

let name = "Luis";
let age = 25;
Copied!

In the previous example,

  • We did not specify the type of the variables name and age
  • However, TypeScript can infer that name is of type string and age is of type number based on the values we assign to them.

TypeScript also infers in more complex cases, such as the return types of functions based on the return value.

function add(a: number, b: number) {
    return a + b; // Type inferred as number
}

let result = add(1, 2); // Type inferred as number
Copied!