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
File organization in TypeScript web projects is critical to keeping code clean and easy to maintain. A well-planned structure not only improves code readability, but also facilitates deployment and team collaboration. In this article, we will explore how to properly set up a web project using TypeScript, separating development files from production files for a more efficient workflow.
When working with TypeScript in web projects, it is tempting to keep all our files in a single folder. However, this practice can lead to confusion as the project grows. The best strategy is to clearly separate the development files from the files that will be deployed in production.
To implement this structure, we need to:
The first step is to set up a folder structure that clearly separates the source code from the public files:
public
: for files to be deployed (HTML, CSS, compiled JS).src
: for source code files (TypeScript files).web-site/├─── public/│ ├─── index.html│ │ └── scripts/│ └── main.js (compiled)└── src/ └── main.ts
This structure allows a clear separation between what is development code and what will be deployed in production. The public
folder will contain everything that needs to be accessible from the browser, while src
will hold our TypeScript code that does not need to be uploaded to the server.
In order for TypeScript to compile files to the correct folder, we need to create and configure a tsconfig.json
file:
src
folder in the terminal.tsc --init
{ " compilerOptions": { " target": "es2016", " module": "commonjs", " outDir": "../public/scripts", " esModuleInterop": true, " forceConsistentCasingInFileNames": true, " strict": true, " skipLibCheck": true}
The key parameter here is "outDir": "../public/scripts"
, which tells TypeScript to place the compiled JavaScript files in the scripts
folder inside public
.
With the configuration set up, we can now compile our TypeScript code by pointing to the configuration file:
tsc -p tsconfig.json
This command will read the configuration and compile all TypeScript files according to the specified rules, placing the resulting JavaScript files in the target folder.
Finally, we need to make sure that our HTML file correctly points to the compiled JavaScript files:
<!-- Before --><script src="main.js"></script>
<!-- After --><script src="./scripts/main.js"></script>
This update ensures that the browser loads the JavaScript file from the correct location.
The separation between development and production files offers multiple advantages:
public
folder needs to be uploaded to the server.This structure also facilitates integration with more advanced build tools such as Webpack or Parcel if you decide to scale your project in the future.
Proper file organization in TypeScript web projects is a fundamental step in developing maintainable and scalable applications. Implementing this structure from the beginning of the project will save you time and headaches as your application grows. Have you tried this structure in your projects? Share your experience in the comments and tell us what other organizational practices you use in your developments.
Contributions 2
Questions 0
Want to see more contributions, questions and answers from the community?