TypeScript · Junior · Question 57 of 75

How does instanceof help with types?

if (err instanceof HttpError) {
  console.log(err.status);
}

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).

Further reading

Problem with a question

TypeScript · Junior · question 57

Test yourself

Take the TypeScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz