-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102.ts
277 lines (164 loc) · 4.84 KB
/
102.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// # Unions and Intersections
// ---
// SLIDE
// ---
// ## Basic example
type SingleDigitPrime = 2 | 3 | 5 | 7;
type SingleDigitOdd = 1 | 3 | 5 | 7 | 9;
type SingleDigitPrimeOrOdd = SingleDigitPrime | SingleDigitOdd;
// result: 2 | 3 | 5 | 7 | 1 | 9
type SingleDigitPrimeAndOdd = SingleDigitPrime & SingleDigitOdd;
// result: 3 | 5 | 7
// ---
// SLIDE
// ---
// ## Object union
// Basically an or operator
type Human = { firstName: string; lastName: string; address: string; oib: string; };
interface Company { companyName: string; address: string; oib: string; };
declare function verifyOIB(oib: string): boolean;
// declare = I'm to lazy to write a function body
function payTaxes(entity: Human | Company): void {
verifyOIB(entity.oib);
// entity.companyName;
// result: Property 'companyName' does not exist on type 'Human'.ts(2339)
let name: string;
if ('companyName' in entity) {
// `entity: Company` in this block
name = entity.companyName;
} else {
// `entity: Human` in this block
name = entity.firstName + ' ' + entity.lastName;
}
// other stuff...
}
// ---
// SLIDE
// ---
// ## Object intersection
interface DbModel { id: number; }
type ChildObject = { parentId: number; };
type ChildDbModel = DbModel & ChildObject;
function addChild(parentId: number, child: DbModel): ChildDbModel {
return {
...child,
parentId,
}
}
// ---
// SLIDE
// ---
// ## Narrowing down types with intersections
type Bipedal = { usesFourLegs: false; usesTwoLegs: true; };
type Quadrupedal = { usesFourLegs: true; usesTwoLegs: false; };
type Dinosaur = (Bipedal | Quadrupedal) & {
laysEggs: true;
haveFeathers?: boolean;
canFly?: boolean;
warmBlooded?: boolean;
};
type Theropod = Dinosaur & Bipedal;
const tRex: Theropod = {
canFly: false,
laysEggs: true,
usesFourLegs: false,
usesTwoLegs: true,
};
type Bird = Theropod & { haveFeathers: true; canFly: boolean; warmBlooded: true; };
const chicken: Bird = {
canFly: false,
haveFeathers: true,
laysEggs: true,
usesFourLegs: false,
usesTwoLegs: true,
warmBlooded: true,
};
// ---
// SLIDE
// ---
// ## Side note: optional is not the same as undefined
let _obj7: { a?: string } = {};
// let _obj8: { a: string | undefined } = {};
// result: Property 'a' is missing in type '{}' but required in type '{ a: string | undefined; }'.ts(2741)
// ---
// SLIDE
// ---
// ## interface extends
// Basically the same as object intersections
interface Bird2 extends Theropod {
haveFeathers: true;
canFly: boolean;
warmBlooded: true;
}
const chicken2: Bird2 = chicken;
// But with restrictions
// interface Bird3 extends Dinosaur {}
// result: An interface can only extend an object type or intersection of object types with statically known members.ts(2312)
// ---
// SLIDE
// ---
// ## A "real" union of objects
// Goal: shared types are required and individual ones are optional
type LegalEntity = (Human | Company) & Partial<Human> & Partial<Company>;
function payTaxes2(entity: LegalEntity): void {
verifyOIB(entity.oib);
const name: string = entity.companyName ?? ((entity.firstName ?? '') + ' ' + (entity.lastName ?? ''));
// other stuff...
}
// ---
// SLIDE
// ---
// ## Side note: JS nullish coalescing && optional chaining
function old_getChildId(parent: any): number | null {
if (parent === null || parent === undefined) return null;
if (parent.child === null || parent.child === undefined) return null;
// NOTE: Can't use `parent.child.id || null`, because id can be 0
if (parent.child.id === null || parent.child.id === undefined) return null;
return parent.child.id;
}
function new_getChildId(parent: any): number | null {
return parent?.child?.name ?? null;
}
globalThis.notSureIfThisFunctionExists?.();
// ---
// SLIDE
// ---
// ## Function overloads
function len(s: string): number;
function len(arr: any[]): number;
function len(x: string | any[]) {
return x.length;
}
len('string');
len([1, 2, 3]);
// ---
// SLIDE
// ---
// ## Overloading arrow functions
type Len2 = {
(s: string): number;
(arr: any[]): number;
}
let len2: Len2 = (x: string | any[]) => x.length;
len2('string');
len2([1, 2, 3]);
// ---
// SLIDE
// ---
// ## Overloading arrow functions v2
type Len3 = ((s: string) => number) & ((arr: any[]) => number);
let len3: Len3 = (x: string | any[]) => x.length;
len3('string');
len3([1, 2, 3]);
// ---
// SLIDE
// ---
// ## Side note: If you ever need to type `this`
function socketController(this: SocketControllerContext, socket: any, data: any, ack?: () => void) {
const activeConnection = this.activeConnectionsRepository.findBySocket(socket);
// ...
}
type SocketControllerContext = { activeConnectionsRepository: { findBySocket(socket: any): any }; };
// ---
// NEXT
// ---