no-array-constructor
禁止通用
Array构造函数.
✅
在 ESLint 配置 中扩展"plugin:@typescript-eslint/recommended" 可启用此规则。
🔧
此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复。
🧱
This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.
This rule extends the base no-array-constructor rule from ESLint core. 它增加了对泛型 Array 构造函数 (new Array<Foo>()) 的支持。
¥It adds support for the generically typed Array constructor (new Array<Foo>()).
- ❌ 错误
- ✅ 正确
Array(0, 1, 2);
new Array(0, 1, 2);
Open in PlaygroundArray<number>(0, 1, 2);
new Array<Foo>(x, y, z);
Array(500);
new Array(someOtherArray.length);
Open in Playground选项
See eslint/no-array-constructor's options.
如何使用
- 扁平配置
- 旧版配置
eslint.config.mjs
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"no-array-constructor": "off",
"@typescript-eslint/no-array-constructor": "error"
}
});
.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"no-array-constructor": "off",
"@typescript-eslint/no-array-constructor": "error"
}
};
在线运行试试这个规则 ↗