Recall & Review
beginner
What does the
instanceof operator check in TypeScript?<code>instanceof</code> checks if an object is an instance of a specific class or constructor function at runtime.Click to reveal answer
beginner
Why can't
instanceof be used with interfaces in TypeScript?Interfaces are only a compile-time construct in TypeScript and do not exist in the generated JavaScript code, so
instanceof cannot check them at runtime.Click to reveal answer
intermediate
How can you check if an object implements an interface in TypeScript?
You can check if an object implements an interface by verifying the presence of required properties or methods manually, often called 'duck typing'.
Click to reveal answer
intermediate
What is 'duck typing' in the context of TypeScript interfaces?
'Duck typing' means checking if an object has the necessary properties or methods to be considered of a certain type, rather than checking its actual class or constructor.Click to reveal answer
intermediate
Can you use
instanceof with classes that implement interfaces?Yes, you can use
instanceof with classes because they exist at runtime, but this only checks the class, not the interface itself.Click to reveal answer
What does
instanceof check in TypeScript?✗ Incorrect
instanceof works by checking the prototype chain of an object against a class or constructor function.Why does
instanceof fail with interfaces?✗ Incorrect
Interfaces are a TypeScript-only feature and do not exist in the JavaScript output, so
instanceof cannot check them.How can you check if an object matches an interface?
✗ Incorrect
You manually check if the object has the properties or methods defined by the interface, a technique called duck typing.
Which of these is true about interfaces in TypeScript?
✗ Incorrect
Interfaces are erased during compilation and only help with type checking during development.
Can
instanceof check if an object is from a class that implements an interface?✗ Incorrect
instanceof checks the class or constructor function, but interfaces do not exist at runtime.Explain why the
instanceof operator does not work with interfaces in TypeScript.Think about what exists in the final JavaScript code.
You got /4 concepts.
Describe how you can check if an object implements an interface in TypeScript without using
instanceof.Focus on what you can check on the object itself.
You got /4 concepts.