-
Notifications
You must be signed in to change notification settings - Fork 1
/
genericTypes.ts
53 lines (40 loc) · 1.07 KB
/
genericTypes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* O tipo genérico, é um tipo especial
* capaz de assumir o papel de qualquer tipo
*/
// Recriando o map do JavaScript
const arr: number[] = [1, 2, 3, 4, 5];
arr.map((x) => x * x);
// [1, 4, 9, 16, 25]
function nossoMap<T>(arr: T[], callback: (t: T) => T): T[] {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
const elem = callback(arr[i]);
newArr.push(elem);
}
return newArr;
}
const numeros: number[] = [1, 2, 3, 4, 5];
const numerosAoQuadrado = nossoMap<number>(numeros, (numero) => {
return numero * numero;
});
console.log("numerosAoQuadrado", numerosAoQuadrado);
const nomes: string[] = [
"Dali",
"Lenny",
"Andre",
"Yasmin",
"Camila",
"Mateus",
];
const nomesEmMaiusculo = nossoMap<string>(nomes, (nome) => {
return nome.toUpperCase();
});
console.log("nomesEmMaiusculo", nomesEmMaiusculo);
function convertTypes<T, U>(t: T, callback: (t: T) => U): U {
return callback(t);
}
const convertNumberToString = convertTypes<number, string>(5, (x) => {
return `${x}`;
});
console.log("convertNumberToString", convertNumberToString);