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
Object-oriented programming is one of the most powerful paradigms in software development, and TypeScript provides robust tools to implement it correctly. Classes are fundamental to this approach, providing a clear structure for code inheritance and object creation. Let's explore how to implement classes in TypeScript following best practices.
Although interfaces and classes may appear similar at first glance, they have different implementations and purposes. Interfaces, which we have already explored above, define contracts that classes must fulfill. Classes, on the other hand, provide a more square, defined structure for inheriting code and sharing it with other elements of your project.
The basic syntax for creating a class in TypeScript is fairly straightforward:
class Person { name: string; age: number; developer: boolean;
constructor(name: string, age: number, developer: boolean) { this.name = name; this.age = age; this.developer = developer; }
greet(): string { return `Hello, my name is ${this.name} and I am ${this.age} years old`; } }}
In this example, we have created a Person
class with three properties: name
, age
and developer
. We have also defined a constructor and a greet()
method.
The constructor is a special method that is executed when an instance of the class is created. Its main function is to initialize the properties of the class with the provided values. In our example, the constructor receives three parameters and assigns them to the corresponding properties of the class.
It is important to note the use of this
to refer to the properties of the class, distinguishing them from the constructor parameters. By convention, they are usually named the same, which makes it easier to identify which parameters correspond to which properties.
Once the class is defined, we can create instances of it and use its methods:
let person = new Person("John", 30, true);console.log(person.greet()); // Print: "Hello, my name is John and I'm 30 years old."
Here, we have created a new instance of the Person
class with the values "John", 30 and true. Then, we call the greet()
method of this instance to get a custom message.
A fundamental practice that is often overlooked is to keep each class in its own file. This follows the "one file, one class" principle and makes your code more maintainable and scalable.
To implement this, we need to:
// class.tsexport class Person { name: string; age: number; developer: boolean;
constructor(name: string, age: number, developer: boolean) { this.name = name; this.age = age; this.developer = developer; }
greet(): string { return `Hello, my name is ${this.name} and I am ${this.age} years old`; } }}
// implementation.tsimport { Person } from './class';
let person = new Person("John", 30, true);console.log(person.greet());
This separation allows the class to be agnostic with respect to how it is used, and facilitates its reuse in different parts of your project.
Modularization is key to maintaining clean and organized code. By separating the definition of a class from its implementation, we are following the principle of single responsibility: each file has only one reason to change.
In addition, this practice facilitates:
When you define a class in its own file and export it, you can import it in as many places as you need, implementing it in different ways according to the specific requirements of each part of your application.
Classes in TypeScript offer a powerful and structured way to implement object-oriented programming. By following best practices for organization and modularization, you can create cleaner, more maintainable and reusable code. Have you implemented classes in your TypeScript projects? Share your experiences and questions in the comments.
Contributions 2
Questions 0
Want to see more contributions, questions and answers from the community?