question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

instanceof doesn't work

See original GitHub issue

This should be mentioned under “Limitations” in the readme. The work-around is to compare with the name property instead.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
avesuscommented, Oct 16, 2017
2reactions
stevenvachoncommented, Aug 14, 2017
export default class ValidationError extends Error {
  constructor(invalidations) {
    super('Invalid input');
    this.invalidations = invalidations;
    this.name = 'ValidationError';
  }
}
try {
  throw new ValidationError(['message']);
} catch(error) {
  if (error instanceof ValidationError) {
    console.log('ok');
  } else {
    throw error;
  }
}

You know, I think my current project is not importing this library (due to https://github.com/facebookincubator/create-react-app/issues/2952) Even with it added locally to babel-preset-react-app, it doesn’t log “ok”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript instanceof not working - Stack Overflow
One workaround is to first cast object to the desired class and then check for a property against undefined . class User{ displayName:string;...
Read more >
How to Fix instanceof Not Working For Custom ... - Danny Guo
In that case, instanceof doesn't work, which breaks any logic that is based on whether or not an error is a case of...
Read more >
How to Fix instanceof Not Working For Custom ... - Medium
The recommended fix is to manually set the prototype in the constructor. Any custom errors which further extend DatabaseError still need the same...
Read more >
instanceof not working · Issue #22585 · microsoft/TypeScript
index.ts class HttpError extends Error { } console.log(new HttpError() instanceof HttpError); transpiled with tsc index.ts run with node ...
Read more >
instanceof - JavaScript - MDN Web Docs
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found