Skip to content
← Back to rules

unicorn/no-instanceof-array Pedantic

🛠️ An auto-fix is available for this rule.

作用

要求使用 Array.isArray(),而不是 instanceof Array

为什么这不好?

instanceof Array 检查无法跨 realms/contexts 正常工作。 例如,浏览器中的 frames/windows,或者 Node.js 中的 vm 模块。

示例

以下是此规则的错误代码示例:

javascript
array instanceof Array;
[1, 2, 3] instanceof Array;

以下是此规则的正确代码示例:

javascript
Array.isArray(array);
Array.isArray([1, 2, 3]);

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-instanceof-array": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-instanceof-array": "error",
  },
});
bash
oxlint --deny unicorn/no-instanceof-array

版本

此规则已在 v0.0.8 中添加。

参考资料