Skip to content

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.

const items = [1, , 3]; // Hole at index 1
const values = [, 2, 3]; // Hole at index 0
const data = [1, , , 4]; // Multiple holes

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.