-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects-and-interfaces.ts
More file actions
134 lines (111 loc) · 2.53 KB
/
objects-and-interfaces.ts
File metadata and controls
134 lines (111 loc) · 2.53 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
// Interface declaration
interface Car {
type: string;
model: string;
color: string;
horsePower: number;
}
// Object declaration
const car: Car = {
type: "Fiat",
model: "500", //key "model" and sting value "500"
color: "white",
horsePower: 100,
};
// Interface person object
interface Person {
name: string;
lastName: string;
address: string;
age: number;
hobbies: string[];
greeting: () => string; // function that returns string
// greeting(): string;
}
// Object with method and array value
const person: Person = {
name: "Petya",
lastName: "Petrova",
address: "Vitosha 5",
age: 44,
hobbies: ["spinning", "yoga"],
greeting: function () {
return `Hello! My name is ${person.name}!`;
},
};
// Properties access
person.name;
person.age;
person["address"];
// console.log(person.greeting());
// console.log(person["greeting"]());
// console.log(person);
// console.log(person.address);
// console.log(person.greeting());
// Properties redeclaration
person.age = 35;
// console.log(person);
// person = {
// age: 20,
// name: "Peter",
// };
let dog = { name: "Spark" };
dog.name = "Bobb";
dog = { name: "Nik" };
//Explicit object type
const mouse: { name: string; favFood: string; age: number } = {
name: "Jerry",
favFood: "Cheese",
age: 2,
};
//Interfaces - optional parameters, rwead only and string literals
interface User {
name: string;
readonly email: string;
role: "user" | "admin" | "superadmin" | "superuser";
//optional parameter
job?: string;
password?: string | number;
}
const user: User = {
name: "Tom",
email: "tom@mail.com",
role: "admin",
job: "QA",
password: 12345,
};
//optional parameter added later
user.password = 456987;
// console.log(user);
interface UserPermissions extends User {
permissions: string;
}
const user3: UserPermissions = {
name: "Tom",
email: "tom@mail.com",
role: "admin",
job: "QA",
password: 12345,
permissions: "denied",
};
//exercise sum total price
interface Product {
name: string;
price: number;
getTotalPrice: (quantity: number) => number;
}
const phone: Product = {
name: "Nokia",
price: 10,
getTotalPrice: function (quantity: number) {
return quantity * this.price;
},
};
console.log(phone.getTotalPrice(5));
function orderDetails(quantity: number, product: Product) {
console.log(`Order for: ${product.name}`);
console.log(`Product quantity: ${quantity}`);
console.log(`Product unit price: ${product.price}$`);
console.log(`Total price: ${product.getTotalPrice(quantity)}$`);
}
orderDetails(5, phone);