unicorn/no-instanceof-builtins Suspicious
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 Arrayfails 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:
if (arr instanceof Array) { … }
if (el instanceof HTMLElement) { … }Examples of correct code for this rule:
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"
Controls which built-in constructors are checked.
"loose"(default): Only checks Array, Function, Error (ifuseErrorIsErroris true), and primitive wrappers"strict": Additionally checks Error types, collections, typed arrays, and other built-in constructors
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 in the CLI or using the config file, you can use:
oxlint --deny unicorn/no-instanceof-builtins{
"rules": {
"unicorn/no-instanceof-builtins": "error"
}
}