Fundamentos de TypeScript

1

¿Qué es TypeScript y por qué usarlo?

2

Instalación de Node.js y TypeScript CLI, configuración de tsconfig.json

3

Tipos primitivos: string, number, boolean, null, undefined de Typescript

4

Tipos especiales: any, unknown, never, void de TypeScript

5

Arrays, Tuplas, Enums en TypeScript

Funciones e Interfaces

6

Declaración de funciones, tipado de parámetros y valores de retorno

7

Parámetros opcionales, valores por defecto y sobrecarga de funciones

8

Creación y uso de interfaces de TypeScript

9

Propiedades opcionales, readonly, extensión de interfaces en TypeScript

Clases y Programación Orientada a Objetos

10

Creación de clases y constructores En TypeScript

11

Modificadores de acceso (public, private, protected) en Typescript

12

Uso de extends, sobreescritura de métodos en TypeScript

13

Introducción a Genéricos en Typescript

14

Restricciones con extends, genéricos en interfaces

Módulos y Proyectos

15

Importación y exportación de módulos en TypeScript

16

Agregando mi archivo de Typescript a un sitio web

17

Configuración de un proyecto Web con TypeScript

18

Selección de elementos, eventos, tipado en querySelector en TypeScript

19

Crear un proyecto de React.js con Typescript

20

Crea un proyecto con Angular y Typescript

21

Crea una API con Typescript y Express.js

Conceptos Avanzados

22

Introducción a types en TypeScript

23

Implementación de Decoradores de TypeScript

24

Async/await en Typescript

25

Pruebas unitarias con Jest y TypeScript

26

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

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
13 Hrs
47 Min
32 Seg
Curso de TypeScript

Curso de TypeScript

Amin Espinoza

Amin Espinoza

Agregando mi archivo de Typescript a un sitio web

16/26
Resources

Integrating TypeScript into web projects is a fundamental skill for modern developers. This programming language, which enhances JavaScript typing, can significantly enhance your web applications by providing early error detection and better code documentation. Learning how to unify TypeScript with HTML will allow you to take advantage of the best of both worlds: the static typing of TypeScript and the universality of JavaScript in the browser.

How to integrate TypeScript into a web project?

So far, you may have worked with TypeScript in isolation, but the true power of this language manifests itself when you incorporate it into real web projects. Integration is surprisingly simple and requires only a few steps.

To begin, we need to create a basic structure for our web project:

  1. Create a folder called "web" within your TypeScript project.
  2. Inside this folder, create a basic HTML file called "index.html".
  3. Add the standard HTML structure to this file.

The HTML can be as simple as this:

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>TypeScriptWeb Project</title></head></head><body> <!-- Your HTML content here -->    
 <script src="main.js"></script></body></html></html>

It is important to note that in the HTML we are referencing a JavaScript file(main.js), not a TypeScript file. This is because browsers cannot directly interpret TypeScript files.

How to convert TypeScript to JavaScript for the browser?

The next step is to create a TypeScript file that we will then compile to JavaScript:

  1. Create a file called "main.ts" in the same folder as your HTML.
  2. Write some simple TypeScript code, for example:
console.log("Hello from my browser");
  1. Compile the TypeScript file to JavaScript using the TypeScript compiler (tsc):
tsc main.ts

This command will generate a main.js file which is exactly what our HTML is expecting. The magic of TypeScript is that it allows us to write statically typed code, but then compile it to pure JavaScript that any browser can understand.

How to visualize and test your web project with TypeScript?

Once you have your HTML and your TypeScript file compiled, you need a way to see your project in action. There are several Visual Studio Code extensions that make this process easy:

Live Server or Live Preview.

These extensions allow you to run a local server to view your web project in real time:

  1. Live Server from Ritwick Dey: With over 60 million downloads, this popular extension adds a "Go Live" button to VS Code.
  2. Microsoft'sLive Preview: An alternative that provides an integrated preview within the editor.

To use these extensions:

  1. Install one of them from the VS Code marketplace.
  2. Open your HTML file.
  3. Click the "Go Live" or "Show Preview" button depending on the extension you have installed.
  4. A local URL (similar to localhost) will open showing your web page.

Verifying TypeScript integration

To confirm that your TypeScript code is working correctly:

  1. Open the browser's developer tools (F12 or right click → Inspect).
  2. Go to the "Console" tab.
  3. You should see the "Hello from my browser" message you defined in your TypeScript file.

This process demonstrates that TypeScript has transferred its functionality to JavaScript, and this code is being executed correctly by the browser through the HTML file.

Why is this integration important?

The ability to unify TypeScript with traditional web projects offers numerous advantages:

  • Improved error detection: TypeScript identifies potential problems before they reach the browser.
  • Improved auto-completion: Code editors provide more accurate suggestions thanks to the type system.
  • Safer refactoring: Code changes are safer with type checking.
  • Implicit documentation: Types serve as documentation for other developers.

This integration does not represent an obstacle in web application development, but provides an additional layer of security and efficiency in your workflow.

Combining TypeScript with HTML is just the beginning. As you progress, you will be able to integrate frameworks such as React, Angular or Vue.js, all of which benefit greatly from TypeScript's type system.

Have you tried integrating TypeScript into your web projects? Share your experiences and any challenges you've encountered with this integration. Your perspective might help other developers who are just starting out on this journey.

Contributions 2

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

![](https://static.platzi.com/media/user_upload/upload-4f95c8b3-909b-4bfb-b86e-26f910a5e850.png)
para sacar la consola se usa f12 o ctrl +shift + c