TypeScript · Junior · Question 57 of 75
How does instanceof help with types?
if (err instanceof HttpError) {
console.log(err.status);
}- ✓Correct answer: It narrows the value to the class type inside the branch
- 2It converts the value to the class
- 3It works only with interfaces
Explanation
instanceof checks the prototype chain at runtime, so it works with classes, not with interfaces or type aliases (they don't exist at runtime).