eslint-plugin-unicorn: Rule proposal: `no-instanceof-error`

Disallow instanceof Error checks.

Fail

const isError = foo instanceof Error;

Pass

const isError = Object.prototype.toString.call(foo) === '[object Error]'

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 2
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Too general. It’s generally fine to instanceof your own types, just not built-ins.

Is it though? If I load the same library in 2 realms I have the same issue. It’s just that built-ins are more common.

instanceof is never safe, it’s just easy to use until you have to check something cross-realm.

Example

document instanceof Object
// true

document.querySelector('iframe').contentWindow.document instanceof Object
// false

I think if you want to add this rule it should be a configurable no-instanceof and the default to Error and whatever you encounter the most often.

This rule is accepted.

Correct

Sounds good

Yeah, I think we should just do Error for now. Error is usually the problematic global.