no-empty-array-literal
Disallow bare [] in places where the rule wants new Array() or new Array<T>() instead.
Rule details Problem
Section titled “Rule details ”ProblemThis 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.
Options
Section titled “Options”Main options
Section titled “Main options”inferTypeForEmptyArrayFix, defaultfalse, lets the rule ask TypeScript for a contextual element type before fixingrequireExplicitGenericOnNewArray, defaulttrue, keepsnew Array<T>()when the element type is knownallowedEmptyArrayContexts, all keys default totrue, controls which safe contexts can still use[]ignoreInferredNonEmptyLiterals, defaulttrue, accepted but has no effect
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, }, ], }, },];Allowed contexts
Section titled “Allowed contexts”allowedEmptyArrayContexts supports these keys:
arrowFunctionBodyassignmentExpressionsassignmentPatternscallArgumentsconditionalExpressionsforOfStatementsjsxAttributeslogicalExpressionspropertyValuesreturnStatementstypeAssertions
Examples
Section titled “Examples” 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];Related rules
Section titled “Related rules” array-type-generic Enforce `Array` and `ReadonlyArray` type syntax.
no-array-constructor-elements Check how `new Array(...)` is used once you adopt constructor form.