no-empty-array-literal
errorFixableType-aware
Disallow bare [] in places where the rule wants new Array() or new Array<T>() instead.
Rule details Problem
Section titled “Rule details Problem ”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.
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 "@pobammer-ts/eslint-cease-nonsense-rules";
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-genericEnforce `Array` and `ReadonlyArray` type syntax.
no-array-constructor-elementsCheck how `new Array(...)` is used once you adopt constructor form.
