Skip to content
← Back to rules

unicorn/no-instanceof-builtins Suspicious

💡 A suggestion is available for this rule for some violations.

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) { … }

Configuration ​

This rule accepts a configuration object with the following properties:

exclude ​

type: string[]

default: []

Constructor names to exclude from checking.

include ​

type: string[]

default: []

Additional constructor names to check beyond the default set. Use this to extend the rule with additional constructors.

strategy ​

type: "strict" | "loose"

default: "loose"

Controls which built-in constructors are checked.

"strict" ​

Additionally checks Error types, collections, typed arrays, and other built-in constructors.

"loose" ​

Only checks Array, Function, Error (if useErrorIsError is true), and primitive wrappers.

useErrorIsError ​

type: boolean

default: false

When true, checks instanceof Error and suggests using Error.isError() instead. Requires the Error.isError() function to be available.

How to use ​

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

json
{
  "rules": {
    "unicorn/no-instanceof-builtins": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-instanceof-builtins": "error",
  },
});
bash
oxlint --deny unicorn/no-instanceof-builtins

Version ​

This rule was added in v0.16.12.

References ​