コンテンツにスキップ

useIsArray

このコンテンツはまだ日本語訳がありません。

Diagnostic Category: lint/suspicious/useIsArray

Since: v1.0.0

Sources:

Use Array.isArray() instead of instanceof Array.

In JavaScript some array-like objects such as arguments are not instances of the Array class. /// Moreover, the global Array class can be different between two execution contexts. For instance, two frames in a web browser have a distinct Array class. Passing arrays across these contexts, results in arrays that are not instances of the contextual global Array class. To avoid these issues, use Array.isArray() instead of instanceof Array. See the MDN docs for more details.

const xs = [];
if (xs instanceof Array) {}
code-block.js:2:5 lint/suspicious/useIsArray  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use Array.isArray() instead of instanceof Array.

1 │ const xs = [];
> 2 │ if (xs instanceof Array) {}
^^^^^^^^^^^^^^^^^^^
3 │

instanceof Array returns false for array-like objects and arrays from other execution contexts.

Unsafe fix: Use Array.isArray() instead.

1 1 const xs = [];
2 - if·(xs·instanceof·Array)·{}
2+ if·(Array.isArray(xs))·{}
3 3

const xs = [];
if (Array.isArray(xs)) {}