What does the instanceof operator do in TypeScript?
It checks if an object is an instance of a specific class or constructor function, helping TypeScript narrow the type within a conditional block.
Click to reveal answer
beginner
How does instanceof help with type safety?
By verifying an object's class at runtime, <code>instanceof</code> lets TypeScript know the exact type, so you can safely access properties or methods specific to that class.
Click to reveal answer
beginner
Example: What type does TypeScript infer inside this block?
if (obj instanceof Date) { // What type is obj here? }
Inside the block, TypeScript knows obj is a Date object, so you can use Date methods safely.
Click to reveal answer
intermediate
Can instanceof be used with interfaces in TypeScript?
No, because interfaces do not exist at runtime. instanceof works only with classes or constructor functions that exist when the code runs.
Click to reveal answer
intermediate
Why might you prefer instanceof over checking a property manually?
Because instanceof is a built-in, reliable way to check an object's type, reducing errors and making your code clearer and easier to maintain.
Click to reveal answer
What does obj instanceof ClassName check?
AIf obj is created by ClassName or inherits from it
BIf obj has a property named ClassName
CIf obj is equal to ClassName
DIf obj is a primitive type
✗ Incorrect
instanceof checks if the object was created by the class or inherits from it.
Can instanceof be used to check if an object implements an interface?
AOnly if the interface has a constructor
BYes, always
CNo, interfaces are erased at runtime
DOnly in strict mode
✗ Incorrect
Interfaces do not exist at runtime, so instanceof cannot check them.
Inside if (obj instanceof Date) { ... }, what type does TypeScript assume for obj?
ADate
Bany
CObject
Dunknown
✗ Incorrect
TypeScript narrows obj to Date inside the block.
Which of these is a correct use of instanceof?
Aif (value instanceof string) { ... }
Bif (value instanceof boolean) { ... }
Cif (value instanceof number) { ... }
Dif (value instanceof Array) { ... }
✗ Incorrect
Array is a class, so instanceof works. Primitive types like string, number, boolean are not classes.
Why is instanceof preferred over checking a property like obj.type === 'Date'?
ABecause <code>instanceof</code> is faster
BBecause <code>instanceof</code> checks the actual prototype chain
CBecause properties can never be trusted
DBecause <code>instanceof</code> works with primitives
✗ Incorrect
instanceof checks the real prototype chain, making it more reliable than property checks.
Explain how instanceof helps TypeScript narrow types during runtime checks.
Think about how JavaScript knows an object's class.
You got /4 concepts.
Why can't instanceof be used with interfaces in TypeScript? What alternative can you use to check types related to interfaces?
Interfaces disappear after compiling to JavaScript.
You got /3 concepts.
Practice
(1/5)
1. What does the instanceof operator check in TypeScript?
easy
A. If a variable is a primitive type
B. If an object is an instance of a specific class
C. If a function returns a boolean
D. If a type is an interface
Solution
Step 1: Understand the purpose of instanceof
The instanceof operator checks whether an object was created from a particular class.
Step 2: Differentiate from other checks
It does not check primitive types, function returns, or interfaces, only class instances.
Final Answer:
If an object is an instance of a specific class -> Option B
Quick Check:
instanceof checks class instance = A [OK]
Hint: Remember: instanceof works only with classes, not interfaces [OK]
Common Mistakes:
Thinking instanceof works with interfaces
Confusing instanceof with typeof
Assuming instanceof checks primitive types
2. Which of the following is the correct syntax to check if obj is an instance of class Car?
easy
A. if (obj instanceof Car) { }
B. if (obj instance Car) { }
C. if (obj is Car) { }
D. if (obj type Car) { }
Solution
Step 1: Recall correct instanceof syntax
The correct syntax uses the keyword instanceof between the object and the class name.
Step 2: Identify invalid options
Options B, C, and D use incorrect keywords or missing syntax.
Final Answer:
if (obj instanceof Car) { } -> Option A
Quick Check:
Correct syntax uses instanceof keyword = A [OK]
Hint: Use 'instanceof' keyword exactly between object and class [OK]
Common Mistakes:
Using 'is' or 'type' instead of 'instanceof'
Omitting parentheses around condition
Writing 'instance' instead of 'instanceof'
3. What will be the output of this code?
class Dog { bark() { return 'woof'; } } class Cat { meow() { return 'meow'; } } function speak(animal: Dog | Cat) { if (animal instanceof Dog) { return animal.bark(); } else { return animal.meow(); } } console.log(speak(new Cat()));
medium
A. 'woof'
B. TypeError at runtime
C. 'meow'
D. undefined
Solution
Step 1: Identify the object passed
The function speak is called with new Cat(), so animal is a Cat instance.
Step 2: Check the instanceof condition
The condition animal instanceof Dog is false, so the else block runs, returning animal.meow().
Final Answer:
'meow' -> Option C
Quick Check:
Cat instance triggers else block = D [OK]
Hint: Trace instanceof condition to pick correct method call [OK]
Common Mistakes:
Assuming instanceof works with interfaces
Confusing which method runs for which class
Expecting 'woof' when passing Cat instance
4. Identify the error in this code snippet:
interface Bird { fly(): void; } class Eagle implements Bird { fly() { console.log('Flying'); } } function check(bird: Bird) { if (bird instanceof Eagle) { bird.fly(); } }
medium
A. No error, code is correct
B. Missing return type in fly() method
C. Cannot use instanceof with interfaces
D. Eagle class must be abstract
Solution
Step 1: Understand instanceof usage
The instanceof operator is used with the class Eagle, which is valid even though bird is typed as the interface Bird.
Step 2: Verify no errors
There are no syntax errors, type mismatches, or runtime issues. TypeScript compiles this code and narrows bird to Eagle inside the if block.
Final Answer:
No error, code is correct -> Option A
Quick Check:
instanceof with class on interface-typed param works = D [OK]
Hint: Remember: instanceof works with classes even on interface-typed variables [OK]
Common Mistakes:
Thinking instanceof can't be used on interface-typed variables
Confusing instanceof with direct interface checks
Thinking implementing class must be abstract
5. Given these classes:
class Animal { name: string; constructor(name: string) { this.name = name; } } class Fish extends Animal { swim() { return 'swimming'; } } class Bird extends Animal { fly() { return 'flying'; } }
Write a function move that takes animal: Animal and returns 'swimming' if it's a Fish, 'flying' if it's a Bird, or 'unknown' otherwise. Which implementation is correct?
hard
A. function move(animal: Animal) { if (animal instanceof Animal) return 'unknown'; else if (animal instanceof Fish) return animal.swim(); else return animal.fly(); }
B. function move(animal: Animal) { if (animal instanceof Fish) return 'flying'; else if (animal instanceof Bird) return 'swimming'; else return 'unknown'; }
C. function move(animal: Animal) { if (animal instanceof Fish) return animal.fly(); else if (animal instanceof Bird) return animal.swim(); else return 'unknown'; }
D. function move(animal: Animal) { if (animal instanceof Fish) return animal.swim(); else if (animal instanceof Bird) return animal.fly(); else return 'unknown'; }
Solution
Step 1: Check instanceof order and method calls
The function must check if animal is a Fish or Bird using instanceof and call the correct method.
Step 2: Validate each option
function move(animal: Animal) { if (animal instanceof Fish) return animal.swim(); else if (animal instanceof Bird) return animal.fly(); else return 'unknown'; } correctly checks Fish first, calls swim(), then Bird with fly(), else returns 'unknown'. function move(animal: Animal) { if (animal instanceof Animal) return 'unknown'; else if (animal instanceof Fish) return animal.swim(); else return animal.fly(); } wrongly checks Animal first, which always matches. function move(animal: Animal) { if (animal instanceof Fish) return animal.fly(); else if (animal instanceof Bird) return animal.swim(); else return 'unknown'; } swaps methods incorrectly. function move(animal: Animal) { if (animal instanceof Fish) return 'flying'; else if (animal instanceof Bird) return 'swimming'; else return 'unknown'; } returns incorrect strings directly without calling methods.
Final Answer:
function move(animal: Animal) { if (animal instanceof Fish) return animal.swim(); else if (animal instanceof Bird) return animal.fly(); else return 'unknown'; } -> Option D
Quick Check:
Correct instanceof checks and method calls = B [OK]
Hint: Check instanceof order and call matching methods [OK]
Common Mistakes:
Checking base class first, blocking subclass checks
Calling wrong methods for classes
Returning strings instead of calling methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.