instanceOfArrays
Reports using
instanceof Arrayinstead ofArray.isArray().
✅ This rule is included in the ts logical presets.
Using instanceof Array to check if a value is an array has two potential issues:
-
Cross-realm failures: In JavaScript, each execution context (such as an iframe or web worker) has its own global scope with its own
Arrayconstructor. When arrays are passed between contexts,instanceof Arrayreturnsfalsebecause the array was created with a differentArrayconstructor. -
Array-like objects: Some objects like
argumentshave array-like behavior but are not instances ofArray.
The Array.isArray() method reliably detects arrays regardless of which realm they were created in.
Examples
Section titled “Examples”if (value instanceof Array) { value.forEach(console.log);}const isArray = data instanceof Array;if (Array.isArray(value)) { value.forEach(console.log);}const isArray = Array.isArray(data);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you are certain that your code will never receive arrays from other execution contexts (such as iframes) and you don’t need to handle array-like objects, you might not need this rule.
Some projects with those characteristics prefer to stick with instanceof for consistency with other class instance checks.