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
Class inheritance in TypeScript is a powerful feature that allows you to extend the functionality of existing classes without modifying their original code. This fundamental technique of object-oriented programming allows you to create more organized class hierarchies and reuse code efficiently, saving time and reducing the possibility of errors in your projects.
Class extension in TypeScript allows us to create a new class that inherits all the properties and methods of a base class, adding new functionality without modifying the original class. This is achieved by means of the keyword extends
.
To illustrate this concept, we will start with a base class called Person
that contains basic properties such as name, age and whether it is a developer:
export class Person { constructor( public name: string, public age: number, public isDeveloper: boolean ) {}}
Now, to extend this class and create a new one called Employee
, we follow these steps:
employee.ts
Person
extends
keyword to inherit from Persona
import { Persona } from './persona';
export class Employee extends Persona { constructor( name: string, age: number, isDeveloper: boolean, public job: string ) { super(name, age, isDeveloper); }
greet() { return `Hello, I work as ${this.job}`; }
getInfo() { return `Iam ${this.age} years old and work as ${this.job}`; } }}
The constructor of the derived class(Employee
) must call the constructor of the base class(Person
) using the super()
keyword. This call is mandatory and must be made before accessing any property with this
inside the constructor.
In our example:
super()
with the parameters needed by the base classpost
property as publicIt is important to note that the derived class inherits all the properties and methods of the base class, but it can also:
post
)greet()
and getInfo()
)Once we have created our class hierarchy, we can use them in our main code. To do this, we create a main.ts
file:
import { Employee } from './employee';
const employee = new Employee('John', 30, true, 'Frontend Developer');
console.log(employee.greet());console.log(employee.getInfo());console.log(employee.name);
When executing this code, we will get:
An interesting feature of TypeScript is cascading compilation. When we compile a file that imports other files, TypeScript automatically compiles all dependent files.
For example, when you run:
tsc main.ts
TypeScript will compile:
main.ts
(the file we specify).employee.ts
(because main.ts
imports it)person.ts
(because employee.ts
imports it)This generates the corresponding JavaScript files(main.js
, employee.js
, person.js
) without the need to compile each one manually. This feature saves time and ensures that all dependencies are correctly compiled.
Class inheritance offers multiple benefits:
Inheritance is especially useful when working with objects that share common characteristics but need specific additional functionality.
Class inheritance in TypeScript is a powerful tool that allows you to create more organized, maintainable and extensible code. Mastering this technique will help you develop more robust applications and take full advantage of object-oriented programming in your TypeScript projects.
Have you used class inheritance in your projects? What other object-oriented programming techniques do you consider fundamental? Share your experience in the comments.
Contributions 4
Questions 0
Want to see more contributions, questions and answers from the community?