Complete the code to check if the object is an instance of the class Dog.
if (pet [1] Dog) { console.log("It's a dog!"); }
The instanceof operator checks if an object is an instance of a class or constructor function.
Complete the function to return true if the pet is a Cat using instanceof.
function isCat(pet: any): pet is Cat {
return pet [1] Cat;
}The instanceof operator is used here to check if pet is an instance of Cat.
Fix the error in the code to correctly check if vehicle is an instance of Truck.
if (vehicle [1] Truck) { vehicle.loadCargo(); }
Only instanceof correctly checks if vehicle is an instance of Truck. Using == or === compares references, not types.
Fill both blanks to create a type guard that checks if pet is a Bird and calls fly().
if (pet [1] Bird) { pet.[2](); }
The instanceof operator checks the type, and fly() is the method called on Bird instances.
Fill all three blanks to create a type guard that checks if shape is a Circle and returns its radius.
function getRadius(shape: Shape): number | null {
if (shape [1] Circle) {
return shape.[2];
} else {
return [3];
}
}instanceof checks if shape is a Circle, then returns its radius. Otherwise, it returns null.