Fundamentos de TypeScript
驴Qu茅 es TypeScript y por qu茅 usarlo?
Instalaci贸n de Node.js y TypeScript CLI, configuraci贸n de tsconfig.json
Tipos primitivos: string, number, boolean, null, undefined de Typescript
Tipos especiales: any, unknown, never, void de TypeScript
Arrays, Tuplas, Enums en TypeScript
Funciones e Interfaces
Declaraci贸n de funciones, tipado de par谩metros y valores de retorno
Par谩metros opcionales, valores por defecto y sobrecarga de funciones
Creaci贸n y uso de interfaces de TypeScript
Propiedades opcionales, readonly, extensi贸n de interfaces en TypeScript
Clases y Programaci贸n Orientada a Objetos
Creaci贸n de clases y constructores En TypeScript
Modificadores de acceso (public, private, protected) en Typescript
Uso de extends, sobreescritura de m茅todos en TypeScript
Introducci贸n a Gen茅ricos en Typescript
Restricciones con extends, gen茅ricos en interfaces
M贸dulos y Proyectos
Importaci贸n y exportaci贸n de m贸dulos en TypeScript
Agregando mi archivo de Typescript a un sitio web
Configuraci贸n de un proyecto Web con TypeScript
Selecci贸n de elementos, eventos, tipado en querySelector en TypeScript
Crear un proyecto de React.js con Typescript
Crea un proyecto con Angular y Typescript
Crea una API con Typescript y Express.js
Conceptos Avanzados
Introducci贸n a types en TypeScript
Implementaci贸n de Decoradores de TypeScript
Async/await en Typescript
Pruebas unitarias con Jest y TypeScript
Principios SOLID, c贸digo limpio, patrones de dise帽o en Typescript
You don't have access to this class
Keep learning! Join and start boosting your career
Data collections in TypeScript are essential for handling multiple values in a structured and efficient way. When working with real applications, we need to organize related information, and TypeScript provides powerful tools to do this with type safety, which reduces errors and improves code maintainability.
When we have multiple variables of the same type, we can group them into collections. TypeScript offers several ways to work with these collections, each with specific features that suit different needs.
Arrays are the most common form of collection in TypeScript. They allow multiple values of the same type to be stored in a single variable.
To declare an array of strings:
let names: string[] = ["Anna", "John", "Mary"];console.log(names);
We can also create arrays of numbers:
let ages: number[] = [39, 25, 30];console.log(ages);
An important feature of arrays is that they can be modified at run time, adding or removing elements as needed.
If we need flexibility in the types of data we store, we can use arrays of type any
:
let mixed: any[] = ["Hello", 42, true];console.log(mixed);
This type of array can contain any data type, which is useful in certain scenarios, but sacrifices the type safety that TypeScript provides.
When we need more complex data structures, we can create arrays based on interfaces:
interface Person { name: string; age: number; developer: boolean;}
let persons: Person[] = [ { { name: "Avin", age: 30, developer: true }];
// We can add interface-compliant elementspersons.push({ name: "Miranda", age: 28, developer: true});
console.log(persons);
These arrays ensure that all elements have the same structure, which makes the code more predictable and easier to maintain.
Tuples are collections with a fixed number of elements, where each item can have a specific type:
let personTuple: [string, number, boolean] = ["Avin", 30, true];console.log(personTuple);
The crucial difference between tuples and arrays is that tuples cannot be modified after their creation. Once defined, their structure remains immutable, which provides greater security in certain scenarios.
To step through the elements of a tuple, we can use a forEach loop:
let [name, age, developer] = personTuple;console.log(name);console.log(age);console.log(developer);
Enumerators (enums) are a powerful feature that allows you to define a set of named constants:
enum DayWeek { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}
let day: DayWeek = DayWeek.Sunday;console.log(day); // Sample: 6 (numeric index)console.log(DayWeek[day]); // Sample: "Sunday" (name).
Enumerators are especially useful for limiting the available options and making the code more readable. If we try to assign a value that is not in the enumerator, TypeScript will display an error:
// This would generate an error// let diaInvalid: DayWeek = DayWeek.January;
The use of typed collections in TypeScript offers multiple advantages:
Working with data collections in TypeScript is more comfortable thanks to strong typing. This avoids common problems such as inadvertently mixing data types, making our applications more robust and structured.
Typed collections are a fundamental tool for any developer looking to write more maintainable and less error-prone code. Have you experimented with these different ways of handling collections in your projects? Share your experiences and doubts in the comments.
Contributions 5
Questions 0
Want to see more contributions, questions and answers from the community?