-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample2.ts
More file actions
159 lines (119 loc) · 2.91 KB
/
Copy pathsample2.ts
File metadata and controls
159 lines (119 loc) · 2.91 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// type User = {
// name: string;
// age: number;
// };
// type Role = {
// role: "admin" | "user";
// };
// type UserWithRole = User & Role;
interface IUser {
name: string;
age: number;
}
interface IUserWithRole extends IUser {
role: "admin" | "user";
}
type IsBoolean = boolean;
const isAdmin: IsBoolean = false;
const isStudent: IsBoolean = true;
interface IFriends {
[index: number]: string;
}
type Add = (num1: number, num2: number) => number;
// Type Guard , Type assertion
let anything: any;
anything = "Mezba";
anything = false;
anything = 22;
//(anything as number).
const calculateDeliveryFee = (distance: string | number | null) => {
if (typeof distance === "number") {
return distance * 20;
} else if (typeof distance === "string") {
const [value] = distance.split(" ");
return Number(value) * 20;
}
};
//const result1 = calculateDeliveryFee(5);
//const result2 = calculateDeliveryFee("5 km");
//const result3 = calculateDeliveryFee(null);
// const friends: string[] = ["Mim", "Fahim", "Tamim"];
// const rollNumbers: number[] = [1, 2, 3];
// const isEligible: boolean[] = [true, false, true];
//generic > generalize
type GenArray<T> = Array<T>; // string[]
const friends: GenArray<string> = ["Mim", "Fahim", "Tamim"];
const rollNumbers: GenArray<number> = [1, 2, 3];
const isEligible: GenArray<boolean> = [true, false, true];
type Coordinates<X, Y> = [X, Y];
const coordinates1: Coordinates<number, number> = [10, 20];
const coordinates: Coordinates<string, string> = ["10", "20"];
// const add: Add = (x, y, z) => {
// return x+y+z;
// };
// add(3, 8)
// constraint: id, name
const addCustomerToOrder = <T extends { id: number; name: string }>(
customerInfo: T,
) => {
return {
orderStatus: "pending",
...customerInfo,
};
};
const customer1 = {
id: 222,
name: "Ashraful",
hasCoupon: true,
};
type CommonCustomerproperties = {
id: number;
name: string;
hasCoupon: boolean;
};
const result = addCustomerToOrder<{
id: number;
name: string;
hasCoupon: boolean;
}>(customer1);
const result2 = addCustomerToOrder<
CommonCustomerproperties & { moneyBag: number }
>({
id: 333,
name: "Tariq",
hasCoupon: false,
moneyBag: 20000,
});
const result3 = addCustomerToOrder<{ id: number; name: string; emni: string }>({
id: 444,
name: "Mezba",
emni: "emni",
});
//console.log(result);
type FoodMenu = {
burger: string; // key : value
pizza: string;
fuchka: string;
};
const myfavFood: keyof FoodMenu = "burger";
type UserWithRole = {
name: string;
age: number;
role: string;
};
//type X = keyof UserWithRole; //'name' |'age'| 'role'
const user1: UserWithRole = {
name: "Mr. X",
age: 30,
role: "admin",
};
const product = {
brand: "Toyota",
modelYear: "1997",
};
const myName = user1["name"];
const getPropertyFromObj = <X>(obj: X, key: keyof X) => {
return obj[key];
};
const res = getPropertyFromObj(user1, "name");
console.log(res);