useArrayLiterals
¥Summary
-
规则生效日期:
v1.7.2¥Rule available since:
v1.7.2 -
诊断类别:
lint/style/useArrayLiterals¥Diagnostic Category:
lint/style/useArrayLiterals -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
no-array-constructor相同¥Same as
no-array-constructor
-
¥How to configure
{ "linter": { "rules": { "style": { "useArrayLiterals": "error" } } }}¥Description
禁止数组构造函数。
¥Disallow Array constructors.
通常不鼓励使用 Array 构造函数来构造新数组,而应使用数组文字表示法,因为存在单参数陷阱,并且 Array 全局变量可能会被重新定义。例外情况是,当 Array 构造函数通过为构造函数提供单个数字参数有意创建指定大小的稀疏数组时。
¥Use of the Array constructor to construct a new array is generally discouraged in favor of array literal notation because of the single-argument pitfall and because the Array global may be redefined. The exception is when the Array constructor intentionally creates sparse arrays of a specified size by giving the constructor a single numeric argument.
¥Examples
¥Invalid
const xs = Array();code-block.js:1:12 lint/style/useArrayLiterals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use an array literal instead of the Array constructor.
> 1 │ const xs = Array();
│ ^^^^^^^
2 │
ℹ The Array constructor is misleading because it can be used to preallocate an array of a given length or to create an array with a given list of elements.
ℹ Safe fix: Use an array literal.
1 │ - const·xs·=·Array();
1 │ + const·xs·=·[];
2 2 │
const xs = Array(0, 1, 2);code-block.js:1:12 lint/style/useArrayLiterals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use an array literal instead of the Array constructor.
> 1 │ const xs = Array(0, 1, 2);
│ ^^^^^^^^^^^^^^
2 │
ℹ The Array constructor is misleading because it can be used to preallocate an array of a given length or to create an array with a given list of elements.
ℹ Safe fix: Use an array literal.
1 │ - const·xs·=·Array(0,·1,·2);
1 │ + const·xs·=·[0,·1,·2];
2 2 │
const xs = new Array(0, 1, 2);code-block.js:1:12 lint/style/useArrayLiterals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use an array literal instead of the Array constructor.
> 1 │ const xs = new Array(0, 1, 2);
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ The Array constructor is misleading because it can be used to preallocate an array of a given length or to create an array with a given list of elements.
ℹ Safe fix: Use an array literal.
1 │ - const·xs·=·new·Array(0,·1,·2);
1 │ + const·xs·=·[0,·1,·2];
2 2 │
const xs = Array(...args);code-block.js:1:12 lint/style/useArrayLiterals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use an array literal instead of the Array constructor.
> 1 │ const xs = Array(…args);
│ ^^^^^^^^^^^^^^
2 │
ℹ The Array constructor is misleading because it can be used to preallocate an array of a given length or to create an array with a given list of elements.
ℹ Safe fix: Use an array literal.
1 │ - const·xs·=·Array(...args);
1 │ + const·xs·=·[...args];
2 2 │
const xs = new Array<number>()code-block.ts:1:12 lint/style/useArrayLiterals FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use an array literal instead of the Array constructor.
> 1 │ const xs = new Array<number>()
│ ^^^^^^^^^^^^^^^^^^^
2 │
ℹ The Array constructor is misleading because it can be used to preallocate an array of a given length or to create an array with a given list of elements.
ℹ Safe fix: Use an array literal.
1 │ - const·xs·=·new·Array<number>()
1 │ + const·xs:·number[]·=·[]
2 2 │
¥Valid
const xs = Array(65000);const xs = [0, 1, 2];¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号