-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-7.ts
More file actions
185 lines (162 loc) · 3.67 KB
/
Copy pathbasic-7.ts
File metadata and controls
185 lines (162 loc) · 3.67 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
let a: number = 5;
let b: number = 10;
let c: number = a + b;
console.log(c);
// Object
let person: { name: string; age: number } = {
name: "Alice",
age: 30
};
console.log(person.name, person.age);
const chai = {
name: "Masala Chai",
price: 20,
isHost: 2
}
/*
{
name: string;
price: number;
isHost: boolean;
}
*/
// Declaring Object Type
let ChaiType: {
name: string;
price: number;
isHost: boolean;
}
ChaiType = {
name: "Ginger Chai",
price: 25,
isHost: true
}
type ChaiTypeAlias = {
name: string;
price: number;
ingredients: string[];
}
let ChaiType2: ChaiTypeAlias = {
name: "Cardamom Chai",
price: 30,
ingredients: ["Cardamom", "Tea Leaves", "Milk", "Sugar"]
}
console.log(ChaiType2);
type Cup ={
size: string;
}
let myCup: Cup = {
size: "Medium"
}
console.log(myCup);
let bigCup = {
size: "200ml",
material: "Ceramic"
} as Cup;
myCup = bigCup;
console.log(bigCup);
type Brew = {brewTime: number;}
const coffee = {brewTime: 5,temperature: 90,beans: "Arabica"}
const chaiBrew: Brew = coffee;
console.log(chaiBrew);
console.log(ChaiType);
console.log(chai);
/*
console.log(a);
console.log(b);
console.log(c);
*/
type User = {
username: string;
email: string;
password: string;
}
let user1: User = {
username: "john_doe",
email: "john@example.com",
// password: "secure123"
password: "secure123"
};
console.log(user1);
let user2: User = {
username: "jane_smith",
email: "jane@example.com",
password: "secure456"
};
console.log(user2);
type Item = { name:string;quantity:number;}
type Address = { street:string;city:string;pincode:number;}
type Order = { id: string;
item: Item[];
address: Address; }
type Chai = { name: string; price: number; isHost: boolean; }
/*
const updatedChai = (updates: Partial<Chai>): Chai => {
return {
name: "Regular Chai",
price: 15,
isHost: false,
...updates
};
}
*/
const updatedChai = (updates: Partial<Chai>) => {
console.log("updating chai with ",updates);
}
updatedChai({price:18});
updatedChai({isHost:true});
updatedChai({});
type ChaiOrder = {
name?: string;
quantity?: number;
}
const placeChaiOrder = (order: Required<ChaiOrder>) => {
console.log(order);
}
placeChaiOrder({name:"Masala Chai",quantity:2});
type ChaiInfo = {
name: string;
price: number;
isHot: boolean;
ingredients: string[];
}
type ChaiNameAndPrice = Pick<ChaiInfo, "name" | "price">;
const chaiDetails: ChaiNameAndPrice = {
name: "Ginger Chai",
price: 25
};
console.log(chaiDetails);
type ReadonlyChai = Readonly<Chai>;
const myChai: ReadonlyChai = {
name: "Lemon Chai",
price: 22,
isHost: false
};
// myChai.price = 25; // Error: Cannot assign to 'price' because it is a read-only property
console.log(myChai);
type ChaiName = Pick<Chai, "name" | "isHost">;
const chaiNameDetails: ChaiName = {
name: "Tulsi Chai",
isHost: true
};
console.log(chaiNameDetails);
type ChaiNewRecipe = {
name: string;
price: number;
isHot: boolean;
secretIngredients: string[];
}
type ChaiWithoutSecret = Omit<ChaiNewRecipe, "secretIngredients">;
const chaiRecipe: ChaiWithoutSecret = {
name: "Herbal Chai",
price: 30,
isHot: true,
secretIngredients: ["Herbs", "Tea Leaves", "Milk", "Sugar"]
};
console.log(chaiRecipe);
type ChaiPrice = Omit<Chai, "isHost">;
const chaiPriceDetails: ChaiPrice = {
name: "Elaichi Chai",
price: 28
};
console.log(chaiPriceDetails);