-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-6.ts
More file actions
145 lines (124 loc) · 3.42 KB
/
Copy pathbasic-6.ts
File metadata and controls
145 lines (124 loc) · 3.42 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
/*
function makeChai(order:{type: string; sugar: number; strong: boolean}) {
// console.log(`Making ${order.type} chai with ${order.sugar} teaspoons of sugar and ${order.strong ? 'strong' : 'weak'} strength.`);
console.log(order);
}
function serveChai(order:{type: string; sugar: number; strong: boolean}) {
// console.log(`Serving ${order.type} chai with ${order.sugar} teaspoons of sugar and ${order.strong ? 'strong' : 'weak'} strength.`);
console.log(order);
}
*/
type ChaiOrder = {
type: string;
sugar: number;
strong: boolean;
};
function makeChai(order: ChaiOrder) {
console.log(order);
}
function serveChai(order: ChaiOrder) {
console.log(order);
}
makeChai({ type: 'Masala', sugar: 2, strong: true });
serveChai({ type: 'Ginger', sugar: 1, strong: false });
type TeaReacipe = {
water: number;
milk: number;
}
class MasalaTea implements TeaReacipe {
water = 200;
milk = 100;
}
/*
// Problem :
type CupSize = 'small' | 'medium' | 'large';
class chai implements CupSize{
}
*/
// Solve:
interface TeaReacipes {
water: number;
milk: number;
}
class MasalaChai implements TeaReacipes {
water = 200;
milk = 100;
}
interface CupSize {
size: 'small' | 'medium' = "medium" | 'large' = 'large';
}/*
class chai implements CupSize{
} */
/*
type response = {ok:true} | {ok:false, error:string};
class myResponse implements response { ok : boolean = true; error!: string; }
*/
interface response = {ok:true} | {ok:false, error:string};
class myResponse implements response { ok : boolean = true; error!: string; }
type TeaType = 'Black' | 'Green' | 'Herbal'; // letaral types
function brewTea(t: TeaType) {
console.log(`Brewing a cup of ${t} tea.`);
}
type BaseChai = {
teaLeaves: number;
}
type ExtraIngredients = {
masala:number;
}
type MasalaChaiRecipe = BaseChai & ExtraIngredients;
const myMasalaChai: MasalaChaiRecipe = {
teaLeaves: 5,
masala: 3
};
console.log(myMasalaChai);
/*
type TeaReacipe = {
teaType: string;
milk: boolean;
sugar: number;
spices?: string[];
};
function prepareTea(recipe: TeaReacipe) {
console.log(recipe);
}
prepareTea({ teaType: 'Green', milk: false, sugar: 1 });
*/
/*
type Person = {
name: string;
age: number;
isStudent: boolean;
};
function introduce(person: Person) {
console.log(`Hello, my name is ${person.name}. I am ${person.age} years old and I am ${person.isStudent ? 'a student' : 'not a student'}.`);
}
introduce({ name: 'Alice', age: 23, isStudent: true });
introduce({ name: 'Bob', age: 30, isStudent: false });
*/
type User = {
username: string;
email: string;
bio?: string;
};
const user1: User = {
username: 'john_doe',
email: 'john@example.com',
bio: 'A passionate tea enthusiast.'
};
const user2: User = {
username: 'jane_smith',
email: 'jane@example.com',
bio: undefined
};
type Config = {
readonly appName: string;
version: number;
};
const cfg: Config = {
appName: 'MyApp',
version: 1.0
};
cfg.appName = 'YourApp'; // Error: Cannot assign to 'appName' because it is a read-only property.
cfg.version = 1.1; // Allowed
// cfg.appName = 'NewApp'; // Error: Cannot assign to 'appName' because it is a read-only property.
console.log(cfg);