-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGeneric.ts
More file actions
66 lines (45 loc) · 1.58 KB
/
Generic.ts
File metadata and controls
66 lines (45 loc) · 1.58 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
function identity<T>(arg: T): T {
return arg;
}
// Using the generic function with different types
let output1 = identity<string>("myString");
let output2 = identity<number>(100);
console.log(output1); // Output: myString
console.log(output2); // Output: 100
// interface GenericIdentityFn<T> {
// (arg: T): T;
// }
// function identity<T>(arg: T): T {
// return arg;
// }
// let myIdentity: GenericIdentityFn<number> = identity;
// console.log(myIdentity(10)); // Output: 10
class GenericNumber<T> {
zeroValue: T;
add: (x: T, y: T) => T;
}
let myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = (x, y) => x + y;
console.log(myGenericNumber.add(5, 10)); // Output: 15
let myGenericString = new GenericNumber<string>();
myGenericString.zeroValue = "";
myGenericString.add = (x, y) => x + y;
console.log(myGenericString.add("Hello, ", "world!")); // Output: Hello, world!
interface Lengthwise {
length: number;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
console.log(arg.length); // Now we know it has a .length property, so no more error
return arg;
}
loggingIdentity({ length: 10, value: 3 }); // Output: 10
// Generic Interfaces with Default Types
// You can provide default types for generic parameters.
// interface GenericInterface<T = string> {
// value: T;
// }
// let stringGeneric: GenericInterface = { value: "Hello" };
// let numberGeneric: GenericInterface<number> = { value: 42 };
// console.log(stringGeneric.value); // Output: Hello
// console.log(numberGeneric.value); // Output: 42