sparseArrays
Reports array literals with holes (sparse arrays).
✅ This rule is included in the ts logical
preset.
Sparse arrays contain holes (empty slots) represented by commas without values between them.
These holes behave differently from undefined
values and array methods treat them inconsistently, which can lead to unexpected behavior and bugs.
Using explicit undefined
values makes the intent clear and ensures consistent behavior.
This rule reports on array literals with holes.
Examples
Section titled “Examples”const items = [1, , 3]; // Hole at index 1const values = [, 2, 3]; // Hole at index 0const data = [1, , , 4]; // Multiple holes
const items = [1, undefined, 3];const values = [undefined, 2, 3];const data = [1, undefined, undefined, 4];
// Trailing commas are allowed (not sparse)const list = [1, 2, 3];
// Empty arrays are allowedconst empty = [];
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you intentionally use sparse arrays and understand their behavior, you might choose to disable this rule.
However, explicit undefined
values are generally clearer and more predictable.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
noSparseArray
- Deno:
no-sparse-arrays
- ESLint:
no-sparse-arrays
- Oxlint:
eslint/no-sparse-arrays
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.