-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterfaces.ts
78 lines (63 loc) · 1.43 KB
/
interfaces.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
// example without interfaces
const car = {
name: 'yaris',
brand: 'toyota',
broken: true,
year: 2004,
kms: 13432,
user: 'john doe'
}
const viewDetails = (vehicle: { name: string, brand: string, broken: boolean, year: number, kms: number, user: string }) => {
console.log(vehicle.name);
console.log(vehicle.user);
console.log(vehicle.kms);
};
viewDetails(car);
// example with interfaces
interface Car {
name: string;
brand: string;
broken: boolean;
year: number;
kms: number;
user: string;
}
const viewDetailsNew = (car: Car) => {
console.log(car.brand);
console.log(car.user);
console.log(car.kms);
}
const cardetail: Car = {
name: 'yaris',
brand: 'toyota',
broken: true,
year: 2004,
kms: 13432,
user: 'john doe'
}
viewDetailsNew(cardetail)
// importent use
interface Reportable {
summary(): string;
}
const newCar = {
name: 'yaris',
brand: 'toyota',
broken: true,
summary(): string {
return `Summary: ${this.name} + ${this.brand} + is ${this.broken ? 'borken' : 'not broken'}`
}
}
const drink = {
color: 'black',
name: 'cola',
summary(): string {
return `Summary: ${this.name} + ${this.color}`
}
}
const printSummary = (item: Reportable) => {
console.log(item.summary())
}
printSummary(newCar);
printSummary(drink);
// conclusion : can use single interface for multiple function arguments.