-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.ts
146 lines (128 loc) · 2.29 KB
/
object.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
// inline interface
interface Product {
id: number,
name: string
qty: number,
price: number
}
let catalog: Product = {
id: 1,
name: 'Yakult',
qty: 2,
price: 20000
}
// nested object
interface Item {
id: number,
name: string,
details: ItemDetails
}
interface ItemDetails {
color: string,
weight: number
}
let productItems: Item = {
id: 1,
name: 'Corola',
details: {
color: 'red',
weight: 100
}
}
interface User {
id: number,
name: string,
carlist: {
[key: string]: Car
}
}
interface Car {
name: string,
color: string
}
let fiqry: User = {
id: 1,
name: 'Fiqry choerudin',
carlist: {
car1: {
name: 'Ferari Galardo',
color: 'red'
}
}
}
// OBJECT FUNCTION
interface CarInterface {
id: number,
name: string
detail: CarDetails
}
interface CarDetails {
color: string,
weight: number,
release: number
}
// can also use this
// cars:Array<CarInterface>
let cars: CarInterface[] = [
{
id: 1,
name: 'Subaru',
detail: {
color: 'red',
weight: 200,
release: 2000
}
},
{
id: 2,
name: 'Honda Civic',
detail: {
color: 'black',
weight: 150,
release: 2020
}
},
{
id: 3,
name: 'Avanza',
detail: {
color: 'silver',
weight: 150,
release: 2010
}
},
{
id: 4,
name: 'Skyline',
detail: {
color: 'silver',
weight: 50,
release: 1999
}
}
]
// filter
// show list car with release date 1999
const filterCars = cars.filter(e => e.detail.release == 1999)
console.log(filterCars)
// find
// find car with black color
const findCars = cars.find(e => e.detail.color === 'black')
console.log(findCars)
// destruct
let Person = {
firstName: 'Fiqry',
lastName: 'Choerudin'
}
let { firstName, lastName }: { firstName: string, lastName: string } = Person
// structural type system
// Object
interface Point {
x: number,
y: number
}
function logPoint(p: Point) {
console.log(`${p.x} , ${p.y}`)
}
const point = { x: 12, y: 26 }
logPoint(point)