-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerics.ts
48 lines (35 loc) · 915 Bytes
/
generics.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
console.clear();
// Hello World of Generics
function getLength(arg: string): number {
return arg.length;
}
console.log(getLength("oibebe"));
class UserAccount {
name: string;
id: number;
constructor(name: string, id: number) {
this.name = name;
this.id = id;
}
}
class Victarion<T> {
private value: T;
constructor(value: T) {
this.value = value;
}
getValue(): T {
return this.value;
}
}
const numberBox = new Victarion<number>(42);
const stringBox = new Victarion<string>("oibebe");
const booleanBox = new Victarion<boolean>(true);
console.log(numberBox.getValue()); // 42
console.log(stringBox.getValue()); // oibebe
console.log(booleanBox.getValue()); // true
// Devemos usar:
function loggingIdentity<Type>(arg: Array<Type>): Array<Type> {
console.log(arg.length);
return arg;
}
console.log(loggingIdentity<string>(["oi", "oibebe", "sorria", "detonautas"]));