Language: EN

typescript-variables

Variables in TypeScript

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 that the variable is only accessible within the block where we define it.

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

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

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
}

Block Scope

Variables declared with let or const inside a block (for example, within an if, for, or {} structure) 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

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

It is not recommended to use var nowadays. In fact, I haven’t even included it at the beginning in options. Really, 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 };

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

Type Inference

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

This means that it is not always necessary to explicitly specify the type of a variable, as TypeScript can deduce it by itself.

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

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 assigned 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