oxc-project/oxc · warning · OxcDiagnostic
Use `Array.isArray()` instead of `instanceof Array`.
Error message
Use `Array.isArray()` instead of `instanceof Array`.
What it means
Diagnostic from the oxlint rule `unicorn/no-instanceof-array`. `value instanceof Array` returns false for arrays created in another realm — an iframe's window, a Web Worker postMessage payload, a Node `vm` sandbox — because each realm has its own Array constructor. `Array.isArray()` checks the object's internal array-ness instead of a prototype chain, so it works across realms and the rule requires it.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/no_instanceof_array.rs:10
use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::BinaryOperator;
use crate::{AstNode, context::LintContext, rule::Rule};
fn no_instanceof_array_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Use `Array.isArray()` instead of `instanceof Array`.")
.with_help("The instanceof Array check doesn't work across realms/contexts, for example, frames/windows in browsers or the vm module in Node.js.")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct NoInstanceofArray;
declare_oxc_lint!(
/// ### What it does
///
/// Require `Array.isArray()` instead of `instanceof Array`.
///
/// ### Why is this bad?
///
/// The `instanceof Array` check doesn't work across realms/contexts.
/// For example, frames/windows in browsers or the `vm` module in Node.js.
///
/// ### ExamplesView on GitHub (pinned to e1e7af627c)
Solutions
- Replace with `Array.isArray(data)` — unicorn/no-instanceof-builtins auto-fixes this exact shape with `oxlint --fix`
- If you must also accept array-likes, check them explicitly (iterability, length) instead of loosening the array check
- If single-realm is guaranteed and instanceof is a team convention, disable the rule for that scope
Example fix
// before
if (data instanceof Array) {
process(data);
}
// after
if (Array.isArray(data)) {
process(data);
} Defensive patterns
Strategy: type-guard
Type guard
function isUnknownArray(v: unknown): v is unknown[] {
return Array.isArray(v);
}
// works for arrays from any realm (iframe, worker, vm) Prevention
- Never use instanceof for built-in type checks; use Array.isArray, typeof, Object.prototype.toString
- Sanitize postMessage/worker payloads with Array.isArray at the trust boundary
- Auto-fix legacy `instanceof Array` occurrences via oxlint --fix
When it happens
Trigger: Any binary expression `x instanceof Array` where the right side resolves to the global Array constructor: `if (data instanceof Array)`, `[1,2,3] instanceof Array`, `obj.arr instanceof Array`. Typical with data arriving via postMessage, workers, iframes, or vm.runInNewContext.
Common situations: Browser extensions and embedded iframes exchanging structured data; Node sandboxed plugins; Jest/jsdom tests where arrays cross environment boundaries.
Related errors
- `alert`, `confirm` and `prompt` functions are not allowed
- Avoid calls to the `Array` constructor
- Unexpected redeclaration of read-only global variable.
- Unexpected comma in middle of array
- {} unexpected commas in middle of array
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/48ea2a5b6728c1fc.
Report an issue: GitHub.