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
Modularization in TypeScript is an essential practice for keeping your code organized, scalable and easy to maintain. Breaking your code into smaller modules not only improves readability, but also makes it easier to detect and correct errors, allowing you to create more robust and professional applications.
A "monolith" in programming refers to large volumes of code concentrated in a single file. This practice is generally discouraged for several reasons:
The solution to these problems is modularization - splitting your code into smaller files with specific responsibilities.
Implementing modules in TypeScript is straightforward and follows a similar pattern to the classes we already know. Let's see how to create our first modules:
First, we create a file called calculator.ts
that will contain basic mathematical functions:
export function sum(num1: number, num2: number) { return num1 + num2;}
export function subtraction(num1: number, num2: number) { return num1 - num2;}
export function multiplication(num1: number, num2: number) { return num1 * num2;}
export function division(num1: number, num2: number) { return num1 / num2;}
export const pi = 3.1416;
The export
keyword is critical here, as it indicates that these elements can be imported from other files. You can export:
Once our module is created, we can use it from another file, for example, main.ts
:
import { addition, subtraction, multiplication, division, pi } from './calculator';
console.log(addition(10, 154)); // Result: 164console.log(subtraction(1, pi)); // Using the imported constant.
When compiling main.ts
with the tsc main.ts
command, TypeScript will automatically compile calculator.ts
as well because it is being referenced. This generates the corresponding JavaScript files that we can run with Node.js:
node main.js
The result will show the values calculated by our imported functions.
Modularization offers multiple advantages:
Refactoring is the process of restructuring existing code without changing its external behavior. It is a recommended practice to improve code quality.
To refactor your code into modules:
It is important to mention that you don't need to start with modules. You can first develop a large file that meets your goal and, once it works correctly, refactor it into smaller modules.
Modularization is a fundamental skill for any TypeScript developer looking to create maintainable and scalable applications. Implementing this practice from the beginning of your projects or refactoring existing code will help you build more robust and professional solutions.
Have you tried modularizing any of your projects? Share in the comments how it has improved your code organization and what challenges you have encountered in the process.
Contributions 2
Questions 0
Want to see more contributions, questions and answers from the community?