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.
    ///
    /// ### Examples

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace with `Array.isArray(data)` — unicorn/no-instanceof-builtins auto-fixes this exact shape with `oxlint --fix`
  2. If you must also accept array-likes, check them explicitly (iterability, length) instead of loosening the array check
  3. 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

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


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/48ea2a5b6728c1fc. Report an issue: GitHub.