Skip to content

unicorn/no-instanceof-builtins Suspicious ​

🚧 An auto-fix is still under development.

What it does ​

Disallows the use of instanceof with ECMAScript built-in constructors because:

  • it breaks across execution contexts (iframe, Web Worker, Node VM, etc.);
  • it is often misleading (e.g. instanceof Array fails for a subclass);
  • there is always a clearer and safer alternative (Array.isArray, typeof, Buffer.isBuffer, …).

Why is this bad? ​

instanceof breaks across execution contexts (iframe, Web Worker, Node vm), and may give misleading results for subclasses or exotic objects.

Examples ​

Examples of incorrect code for this rule:

javascript
if (arr instanceof Array) { … }
if (el instanceof HTMLElement) { … }

Examples of correct code for this rule:

javascript
if (Array.isArray(arr)) { … }
if (el?.nodeType === 1) { … }

How to use ​

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/no-instanceof-builtins
json
{
  "rules": {
    "unicorn/no-instanceof-builtins": "error"
  }
}

References ​

Released under the MIT License.