Skip to content

no-empty-array-literal

Disallow bare [] in places where the rule wants new Array() or new Array<T>() instead.

This rule reports empty array literals with no elements.

It does not ban every []. By default, several contexts are allowed, including call arguments, return statements, object property values, JSX attributes, conditional branches, logical fallbacks, for...of right-hand sides, typed assignment defaults, type assertions, and typed variable declarations that already spell out an array type.

When the rule can read an explicit element type, it auto-fixes to new Array<T>() by default. When it cannot, it still reports the literal and offers new Array() as a suggestion.

  • inferTypeForEmptyArrayFix, default false, lets the rule ask TypeScript for a contextual element type before fixing
  • requireExplicitGenericOnNewArray, default true, keeps new Array<T>() when the element type is known
  • allowedEmptyArrayContexts, all keys default to true, controls which safe contexts can still use []
  • ignoreInferredNonEmptyLiterals, default true, accepted but has no effect
eslint.config.ts
import plugin from "eslint-plugin-cease-nonsense";
export default [
{
plugins: { "cease-nonsense": plugin },
rules: {
"cease-nonsense/no-empty-array-literal": [
"error",
{
allowedEmptyArrayContexts: {
callArguments: false,
returnStatements: false,
},
inferTypeForEmptyArrayFix: true,
requireExplicitGenericOnNewArray: true,
},
],
},
},
];

allowedEmptyArrayContexts supports these keys:

  • arrowFunctionBody
  • assignmentExpressions
  • assignmentPatterns
  • callArguments
  • conditionalExpressions
  • forOfStatements
  • jsxAttributes
  • logicalExpressions
  • propertyValues
  • returnStatements
  • typeAssertions
Incorrect
const values = [];
class Store {
items: Array<string> = [];
queue = [];
}
Correct
const values: Array<string> = [];
function readAll(input: Array<number> = []) {
return input;
}
const fallback = data || [];
const seeded = [1, 2, 3];