Skip to content

Commit 33d4aa7

Browse files
committed
update module 2 functions ts
1 parent 5f2e49b commit 33d4aa7

1 file changed

Lines changed: 36 additions & 22 deletions

File tree

02-languages/02-apuntes/02-typescript/103 functions.ts

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
///-- FUNCIONES ***********************************************************************************
22

33
// 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
55
// especifiquemos serán obligatorios, a menos que usemos un modificador de opcionalidad:
66

77
// Veamos un primer ejempo con funciones clásicas:
@@ -60,41 +60,55 @@ const shout: ShoutFunction = (text, upperCase = true) =>
6060
console.log(shout("hi"));
6161

6262

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+
6377
// *** Funciones como argumentos ******************************************************************
6478

6579
// En el caso de pasar funciones como argumentos de otras, podemos tipar en línea dichos argumentos
6680
// del siguiente modo:
6781

68-
const shout = (text: string, getNumExclamation: () => number): string =>
69-
text.toUpperCase() + "!".repeat(getNumExclamation());
82+
const shout = (text: string, exclamationCallback: () => string) =>
83+
text.toUpperCase() + exclamationCallback();
7084

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));
7291

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));
7992

8093

8194
// *** Sobrecarga de funciones ********************************************************************
8295

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);
91101
}
92102

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);
96108

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'
98112

99113

100114
// *** Tipando funciones con interfaces ***********************************************************

0 commit comments

Comments
 (0)