no-array-constructor
Disallow generic
Array
constructors.
✅
Extending"plugin:@typescript-eslint/recommended"
in anESLint configuration enables this rule.
🔧
Some problems reported by this rule are automatically fixable by the--fix
ESLint command line option.
🧱
This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. SeeRules > Extension Rules.
This rule extends the baseno-array-constructor
rule from ESLint core. It adds support for the generically typedArray
constructor (new Array<Foo>()
).
- ❌ Incorrect
- ✅ Correct
Array(0,1,2);
newArray(0,1,2);
Open in PlaygroundArray<number>(0,1,2);
newArray<Foo>(x, y, z);
Array(500);
newArray(someOtherArray.length);
Open in PlaygroundOptions
Seeeslint/no-array-constructor
's options.
How to Use
- Flat Config
- Legacy Config
eslint.config.mjs
exportdefault 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"
}
};
Try this rule in the playground ↗