-
Notifications
You must be signed in to change notification settings - Fork 0
/
generics.ts
89 lines (74 loc) · 1.62 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function getFirstElement<ElementType>(array: ElementType[]): ElementType {
return array[0];
}
const numbers = [1, 2, 3];
const num = getFirstElement(numbers);
const strings = ["1", "2", "3"];
const str = getFirstElement(strings);
// const de = document.querySelector(".input");
// console.log(de?.value);
const de = document.querySelector<HTMLInputElement>(".input");
console.log(de?.value);
const a = [1, 2, 3];
a.map(() => {}); // map has a generic return value void which will be set based on the value you return
const map = new Map<string, number>();
map.set("key1", 1);
type ApiResponse1 = {
data: any;
isError: boolean;
};
const res1: ApiResponse1 = {
data: {
name: "Ashok",
age: 35,
},
isError: false,
};
type ApiResponse2<Data> = {
data: Data;
isError: boolean;
};
const res2: ApiResponse2<{ name: string; age: number }> = {
data: {
name: "Ashok",
age: 35,
},
isError: false,
};
type UserResponse = ApiResponse2<{ name: string; age: number }>;
const res3: UserResponse = {
data: {
name: "Peeta",
age: 35,
},
isError: false,
};
// specify default types
type ApiResponse3<Data = { status: number }> = {
data: Data;
isError: boolean;
};
const res4: ApiResponse3 = {
data: {
status: 200,
},
isError: false,
};
// override default types
const res5: ApiResponse3<{ name: string }> = {
data: {
name: "Ashok",
},
isError: false,
};
// extend objects and defaults
type ApiResponse4<Data extends Object = { status: number }> = {
data: Data;
isError: boolean;
};
const res6: ApiResponse4<{ name: string }> = {
data: {
name: "Peeta",
},
isError: false,
};