-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-guard.ts
43 lines (37 loc) · 878 Bytes
/
type-guard.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
// instanceof Guard
class AnimalClass {
name: String;
spaecies: String;
constructor(name: String, spaecies: String) {
(this.name = name), (this.spaecies = spaecies);
}
makeSound() {
console.log("making Sound ........");
}
}
class Dog extends AnimalClass {
constructor(name: String, spaecies: String) {
super(name, spaecies);
}
makeBarking() {
console.log(" Dog is Barking");
}
}
class Cat extends AnimalClass {
constructor(name: String, spaecies: String) {
super(name, spaecies);
}
makeMeaw() {
console.log(" Cat is Make Meawing");
}
}
function getAnimal(obj: AnimalClass) {
if (obj instanceof Dog) {
obj.makeBarking();
} else if (obj instanceof Cat) {
obj.makeMeaw();
} else obj.makeSound;
}
const animal1 = new Dog("German Shephard", "Dog");
const animal2 = new Cat("MR.Cat", "Cat");
getAnimal(animal2);