Skip to content

Commit 2d8dd08

Browse files
committed
instanceof typeguard in ts
1 parent 6898f10 commit 2d8dd08

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

typescript-masterclass/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,42 @@ const songDurationFromMS = getSongDuration(
293293

294294
console.log(songDurationFromMS);
295295
```
296+
297+
### instanceof typeguard in ts
298+
299+
```ts
300+
class Foo {
301+
bar() {}
302+
}
303+
304+
const bar = new Foo();
305+
306+
console.log(bar);
307+
308+
// console.log(bar instanceof Foo);
309+
// console.log(Object.getPrototypeOf(bar) === Foo.prototype);
310+
311+
class Song {
312+
constructor(public title: string, public duration: number) {}
313+
}
314+
315+
class Playlist {
316+
constructor(public name: string, public songs: Song[]) {}
317+
}
318+
319+
function getItemName(item: Song | Playlist) {
320+
if (item instanceof Song) {
321+
return item.title;
322+
}
323+
return item.name;
324+
}
325+
326+
const songName = getItemName(new Song("wonderful wonderful", 300000));
327+
console.log("Song Name :", songName);
328+
329+
const playlistName = getItemName(
330+
new Playlist("The Best Songs", [new Song("The Man", 30000)])
331+
);
332+
333+
console.log("Playlist Name :", playlistName);
334+
```

typescript-masterclass/src/app.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1+
class Foo {
2+
bar() {}
3+
}
4+
5+
const bar = new Foo();
6+
7+
console.log(bar);
8+
9+
// console.log(bar instanceof Foo);
10+
// console.log(Object.getPrototypeOf(bar) === Foo.prototype);
11+
112
class Song {
2-
constructor(public title: string, public duration: string | number) {}
13+
constructor(public title: string, public duration: number) {}
14+
}
15+
16+
class Playlist {
17+
constructor(public name: string, public songs: Song[]) {}
318
}
419

5-
function getSongDuration(item: Song) {
6-
if (typeof item.duration === "string") {
7-
return item.duration;
20+
function getItemName(item: Song | Playlist) {
21+
if (item instanceof Song) {
22+
return item.title;
823
}
9-
const { duration } = item;
10-
const minutes = Math.floor(duration / 60000);
11-
const seconds = (duration / 1000) % 60;
12-
return `${minutes}:${seconds}`;
24+
return item.name;
1325
}
1426

15-
const songDurationFromString = getSongDuration(
16-
new Song("Wonderful Wonderful", "05:30")
17-
);
18-
console.log(songDurationFromString);
27+
const songName = getItemName(new Song("wonderful wonderful", 300000));
28+
console.log("Song Name :", songName);
1929

20-
const songDurationFromMS = getSongDuration(
21-
new Song("Wonderful Wonderful", 330000)
30+
const playlistName = getItemName(
31+
new Playlist("The Best Songs", [new Song("The Man", 30000)])
2232
);
2333

24-
console.log(songDurationFromMS);
34+
console.log("Playlist Name :", playlistName);

0 commit comments

Comments
 (0)