Language: EN

typescript-tipos-de-datos-basicos

Basic Data Types in TypeScript

The basic types in TypeScript are the fundamental elements that allow defining and restricting the values that a variable can take.

typescript-objetos-primitivos

Basic data types

Numbers

The “number” data type in TypeScript allows us to represent numbers, whether they are integers or decimals. We can declare a variable of type “number” as follows:

let edad: number = 25;

Strings

The “string” data type in TypeScript allows us to represent text. We can declare a variable of type “string” as follows:

let nombre: string = "Luis";

Booleans

The boolean data type in TypeScript allows us to represent true or false values. We can declare a variable of type boolean as follows:

let esMayorDeEdad: boolean = true;

Array

The array data type in TypeScript allows us to store multiple values of the same type in a single variable. We can declare a variable of type array as follows:

let numeros: number[] = [1, 2, 3, 4, 5];

Special data types

Any

The any data type in TypeScript allows us to store any type of value. It is useful when we do not know the data type that a variable will have.

let variable: any = "Hola";
variable = 10;

Void

The void data type in TypeScript is used to represent the absence of a type. It is mainly used in functions that do not return any value.

function saludar(): void {
  console.log("Hola");
}

Null and Undefined

The null and undefined data types in TypeScript represent the absence of a value. We can declare a variable of type null or undefined as follows:

let nulo: null = null;
let indefinido: undefined = undefined;

Never

The never data type in TypeScript is used to represent values that never occur. It is mainly used in functions that throw exceptions or enter infinite loops.

function lanzarError(): never {
  throw new Error("Ocurrió un error");
}