no-instanceof-builtins
Configuration
Rule Details
Disallow instanceof with built-in objects. instanceof can be unreliable across realms, so safer checks such as typeof, Array.isArray(), Object.prototype.toString.call(), or dedicated type helpers are preferred.
This rule can automatically fix Array, Function, and Error checks when useErrorIsError is enabled. Primitive wrapper checks such as String, Number, and Boolean provide suggestions instead of automatic fixes because converting object-wrapper values to typeof checks can change behavior.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Examples of incorrect code for this rule with { "strategy": "strict" }:
Examples of incorrect code for this rule with { "include": ["HTMLElement"] }:
Examples of correct code for this rule with { "exclude": ["String"] }:
Examples of incorrect code for this rule with { "strategy": "strict", "useErrorIsError": true }:
Options
strategy
Type: "loose" | "strict"
Default: "loose"
"loose"reports primitive wrapper constructors,Function,Array, and constructors listed ininclude."strict"also reports built-in constructors such asError,Map,Set,Date, typed arrays,Object, andRegExp.
include
Type: string[]
Default: []
Additional constructor names to report.
exclude
Type: string[]
Default: []
Constructor names to ignore. This takes precedence over strategy, include, and useErrorIsError.
useErrorIsError
Type: boolean
Default: false
When enabled, Error checks are reported with an autofix to Error.isError(value).