-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeNarrowing.ts
41 lines (35 loc) · 888 Bytes
/
typeNarrowing.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
class BassSolo {
flap() {
console.log('bawm ba ba bawm bam')
}
tap() {
console.log('duh duh duru duru duh duh')
}
}
class DrumSolo {
cymbals() {
console.log('tsssss ts tss ts tssss')
}
twinPedals() {
console.log('badabadabadabadabadabada')
}
snareGhosts() {
console.log('phrrrrrrrrrrrr tak tak prrrrtak')
}
}
type Solo = DrumSolo | BassSolo;
function spotlightOn(solo: Solo) {
if (solo instanceof BassSolo) {
solo.flap(); // Autocomplete works as compiler understands it's a bass solo
solo.tap();
solo.tap();
solo.flap();
}
if (solo instanceof DrumSolo) {
solo.snareGhosts(); // Autocomplete works as compiler understands it's a drum solo
solo.cymbals();
solo.twinPedals();
solo.snareGhosts();
solo.cymbals();
}
}