-
Notifications
You must be signed in to change notification settings - Fork 0
/
types-practice.ts
173 lines (148 loc) · 3.33 KB
/
types-practice.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// 1. Singular Types
type Name = string;
type RollNo = number;
type IsActive = boolean;
type Emails = string[];
type RollNos = number[];
// Using above
const firstName: Name = "Apsara";
const emails: Emails = ["hello", "hi", "1"];
const names: Name[] = ["Apsara", "Saya"]; // <= best practice
// 2. Mixed types
type Id = number | string; // || => evaluate, | type or union or enum like
type Cat = "black" | "white" | "grey" | "nepali"; // <= Important
type Mixed = "black" | "red" | 1 | 2 | false;
const cats: Cat[] = ["black"];
const ids: Id[] = [1, "hehe", "hello", "red"];
const mixedThings: Mixed[] = ["black", 1, false];
// Object Types
type NameObject = {
firstName: string;
lastName: string;
};
interface NameObjectI {
firstName: string;
lastName: string;
}
const nameObj: NameObject = {
firstName: "Apsara",
lastName: "Bishwokarma",
};
// Complex type of object
// type Product = {
// id: number | string;
// name: string;
// price: number;
// category: "category1" | "category2";
// rating: {
// count: number;
// rate: number;
// };
// };
// const product1: Product = {
// id: 1,
// name: "book",
// price: 200,
// category: "category1",
// rating: {
// count: 5,
// rate: 4,
// },
// };
type Category = "category1" | "category2";
type Rating = {
count: number;
rate: number;
};
type Variant = {
price: number;
color: string;
};
type Product = {
id: number | string;
name: string;
price: number;
category: Category;
rating: Rating;
variants?: Variant[];
reviews: {
user: string;
rating: number;
}[];
};
const product1: Product = {
id: 1,
name: "book",
price: 200,
category: "category1",
rating: {
count: 5,
rate: 4,
},
variants: [
{
color: "red",
price: 200,
},
],
reviews: [
{
user: "Ram",
rating: 2,
},
],
};
type RatingN = (typeof product1)["rating"];
type VariantN = NonNullable<(typeof product1)["variants"]>[0];
type ReviewN = (typeof product1)["reviews"][0];
const rating1: Rating = {
rate: 2,
count: 5,
};
const category: Product["category"] = "category1";
const review: Product["reviews"][number] = {
rating: 10,
user: "hey",
};
const variant: NonNullable<Product["variants"]>[0] = {
color: "red",
price: 100,
};
//1 . Function Using Product
// function showProduct(product: Product) {
// console.log(product.name);
// console.log({ name: product.name });
// console.log(product);
// }
// showProduct(product1);
// 1.5.
// function showProduct(product: Product) {
// const name = product.name;
// const id = product.id;
// // can't do rest
// console.log(name);
// console.log({ name: name, id });
// }
// showProduct(product1);
// 2. Extracting values in parameters
// function showProduct({name, id, ...rest}: Product) {
// console.log(name);
// console.log({ name: name, id });
// console.log(rest)
// }
// showProduct(product1);
// 3. Giving default value
// function showProduct({name = "default name", id, ...rest}: Product) {
// console.log(name);
// console.log({ name: name, id });
// console.log(rest)
// }
// showProduct(product1);
// 4. Equivalent to above
function showProduct(product: Product) {
const { name = "default name", id, ...rest } = product;
console.log(name);
console.log({ name: name, id });
console.log(rest);
}
showProduct(product1);