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
Parameters in TypeScript functions offer extraordinary flexibility that allows us to optimize our code and make it more efficient. By mastering the different types of parameters, we can create versatile methods that adapt to multiple scenarios without the need to duplicate code. This capability not only improves readability but also significantly reduces development time.
When working with TypeScript, we can make our functions much more adaptable by using different types of parameters. This allows us to write less code that performs more tasks, resulting in more maintainable and efficient applications.
Optional parameters allow us to define arguments that may or may not be provided when calling a function. To indicate that a parameter is optional, we simply add a question mark (?) after the parameter name.
Let's see a practical example:
function greet(name: string, greeting?: string): string { if (greeting) { return `${greeting}, ${name}!`; } else { return `Hello, ${name}`; } }}
console.log(greet("Amin"));console.log(greet("Amin", "Good morning"));
When executing this code, we will get:
The main advantage of optional parameters is that they save us from having to create multiple versions of the same function to handle different cases. A single function can adapt to different situations depending on the arguments it receives.
Multiple parameters (or rest parameters) allow us to pass an indefinite number of arguments to a function. They are indicated by three dots (....) before the parameter name.
function sumAll(...numbers: number[]): number { return numbers.reduce((accumulator, value) => accumulator + value, 0);}
console.log(`Theresult of adding all is ${sumAll(1, 2, 3, 4, 5)}`);
This code will add all the provided numbers (1+2+3+4+5) and display "The result of adding all is 15".
The reduce method used in this example is an elegant way to process all the elements of an array without using a foreach loop. This method accumulates the values as it cycles through the array.
Multiple parameters can be tricky at first, but with practice they become a very powerful tool for creating flexible functions that can handle different amounts of data.
Defaults allow us to assign a default value to a parameter in case none is provided when calling the function.
function goodbye(name: string, goodbye: string = "Goodbye"): string { return `${goodbye}, ${name}`;}
console.log(goodbye("Amin"));console.log(goodbye("Amin", "See you later"));
When executing this code, we will get:
The key difference between optional parameters and default values is that the former may not be present, while the latter will always have a value, either the one provided or the default.
Implementing these types of parameters in our functions allows us to:
Mastering these concepts in TypeScript helps us write more elegant and efficient code, taking full advantage of the language's capabilities to create robust and maintainable applications.
Special parameters in TypeScript are powerful tools that every developer should know and use. Have you implemented any of these types of parameters in your projects? Which one has been most useful to you? Share your experience in the comments.
Contributions 5
Questions 0
Want to see more contributions, questions and answers from the community?