|
1 | 1 | ///-- FUNCIONES *********************************************************************************** |
2 | 2 |
|
3 | 3 | // Tipar una función en TypeScript no es más que especificar los tipos de los argumentos que recibe |
4 | | -// y el tipo del valor que devuelve. Es importante tener en cuenta que aquellos argumentos que |
| 4 | +// y el tipo del valor que devuelve. Es importante tener en cuenta que aquellos parámetros que |
5 | 5 | // especifiquemos serán obligatorios, a menos que usemos un modificador de opcionalidad: |
6 | 6 |
|
7 | 7 | // Veamos un primer ejempo con funciones clásicas: |
@@ -60,41 +60,55 @@ const shout: ShoutFunction = (text, upperCase = true) => |
60 | 60 | console.log(shout("hi")); |
61 | 61 |
|
62 | 62 |
|
| 63 | +// OPCIONAL: Reusando tipados de función con alias |
| 64 | + |
| 65 | +type TextModifierFn = (text: string, modifier?: boolean) => string; |
| 66 | + |
| 67 | +const shout: TextModifierFn = (text, upperCase = false) => |
| 68 | + (upperCase ? text.toUpperCase() : text) + "!!!"; |
| 69 | + |
| 70 | +const hyphenize: TextModifierFn = (text, snake = false) => text.replace(" ", snake ? "_" : "-"); |
| 71 | + |
| 72 | +console.log(shout("hello world", true)); // HELLO WORLD!!! |
| 73 | +console.log(hyphenize("hello world")); // hello-world |
| 74 | +console.log(hyphenize("hello world", true)); // hello_world |
| 75 | + |
| 76 | + |
63 | 77 | // *** Funciones como argumentos ****************************************************************** |
64 | 78 |
|
65 | 79 | // En el caso de pasar funciones como argumentos de otras, podemos tipar en línea dichos argumentos |
66 | 80 | // del siguiente modo: |
67 | 81 |
|
68 | | -const shout = (text: string, getNumExclamation: () => number): string => |
69 | | - text.toUpperCase() + "!".repeat(getNumExclamation()); |
| 82 | +const shout = (text: string, exclamationCallback: () => string) => |
| 83 | + text.toUpperCase() + exclamationCallback(); |
70 | 84 |
|
71 | | -const getRandom = () => Math.ceil(Math.random() * 10); // Este es mi callback. |
| 85 | +const exclamationGenerator = () => "!".repeat(Math.ceil(Math.random() * 10)); // Callback |
| 86 | + |
| 87 | +console.log(shout("WoooW", exclamationGenerator)); |
| 88 | +console.log(shout("WoooW", exclamationGenerator)); |
| 89 | +console.log(shout("WoooW", exclamationGenerator)); |
| 90 | +console.log(shout("WoooW", exclamationGenerator)); |
72 | 91 |
|
73 | | -console.log(shout("WoW", getRandom)); |
74 | | -console.log(shout("WoW", getRandom)); |
75 | | -console.log(shout("WoW", getRandom)); |
76 | | -console.log(shout("WoW", getRandom)); |
77 | | -console.log(shout("WoW", getRandom)); |
78 | | -console.log(shout("WoW", getRandom)); |
79 | 92 |
|
80 | 93 |
|
81 | 94 | // *** Sobrecarga de funciones ******************************************************************** |
82 | 95 |
|
83 | | -function switchType(c: string): number; |
84 | | -function switchType(c: number): string; |
85 | | -function switchType(c: any) { |
86 | | - if (typeof c === "string") { |
87 | | - return Number(c); |
88 | | - } else { |
89 | | - return String(c); |
90 | | - } |
| 96 | +function switchType(arg: number): string; |
| 97 | +function switchType(arg: string): number; |
| 98 | +function switchType(arg: string | number): string | number { |
| 99 | + if (typeof arg === "string") return Number(arg); |
| 100 | + else return String(arg); |
91 | 101 | } |
92 | 102 |
|
93 | | -const r1 = switchType(3); |
94 | | -const r2 = switchType("65"); |
95 | | -const r3 = switchType({}); // [ts] Argument of type '{}' is not assignable to parameter of type 'number' |
| 103 | +const result1 = switchType("105"); |
| 104 | +console.log(result1, typeof result1); |
| 105 | + |
| 106 | +const result2 = switchType(105); |
| 107 | +console.log(result2, typeof result2); |
96 | 108 |
|
97 | | -// ⚠ Es posible sobrecargar funciones con diferente número de argumentos. |
| 109 | +switchType({}); |
| 110 | + // ^^^ [ts] 1/2 Argument of type '{}' is not assignable to parameter of type 'number' |
| 111 | + // ^^^ [ts] 2/2 Argument of type '{}' is not assignable to parameter of type 'string' |
98 | 112 |
|
99 | 113 |
|
100 | 114 | // *** Tipando funciones con interfaces *********************************************************** |
|
0 commit comments