Skip to content

sparseArrays

Reports array literals with holes (sparse arrays).

✅ This rule is included in the ts logical and logicalStrict presets.

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.

AEconsB0iteAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53< [sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#D6DEEB;--1:#403F">53, <, sp ansty="le--0:#F78C6C;--1:#AA09">8<23sp ansty="le--0:#D6DEEB;--1:#403F">53]<; sp ansty="le--0:#919F9F;--1:#5D63">76// Hole at index< 1d ivclassNa="meec-li">AEconsB0valuAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53 [<, sp ansty="le--0:#F78C6C;--1:#AA09">8<22sp ansty="le--0:#D6DEEB;--1:#403F">53<, sp ansty="le--0:#F78C6C;--1:#AA09">8<23sp ansty="le--0:#D6DEEB;--1:#403F">53]<; sp ansty="le--0:#919F9F;--1:#5D63">76// Hole at index< 0d ivclassNa="meec-li">AEconsB0daAE< =sp ansty="le--0:#D6DEEB;--1:#403F">53< [sp ansty="le--0:#F78C6C;--1:#AA09">8<21sp ansty="le--0:#D6DEEB;--1:#403F">53, , <, sp ansty="le--0:#F78C6C;--1:#AA09">8<24sp ansty="le--0:#D6DEEB;--1:#403F">53]<; sp ansty="le--0:#919F9F;--1:#5D63">76// Multiple hol
44AEco61B0i443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#82AAFF;--1:#3B">61B0undef3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9823 spans="tyle--0:#D6DEEB;--1:#40">3F<53] ;divclass="Nameec-">44AEco61B0va443F<53 [ spans="tyle--0:#82AAFF;--1:#3B">61B0undef3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9822 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9823 spans="tyle--0:#D6DEEB;--1:#40">3F<53] ;divclass="Nameec-">44AEco61B0443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#82AAFF;--1:#3B">61B0undef3F<53, spans="tyle--0:#82AAFF;--1:#3B">61B0undef3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9824 spans="tyle--0:#D6DEEB;--1:#40">3F<53] ;divclass="Nameec-">c6376// Trailing commas are allowed (not spa44AEco61B0443F<53 [ spans="tyle--0:#F78C6C;--1:#AA">0<9821 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9822 spans="tyle--0:#D6DEEB;--1:#40">3F<53, spans="tyle--0:#F78C6C;--1:#AA">0<9823 spans="tyle--0:#D6DEEB;--1:#40">3F<53] ;divclass="Nameec-">c6376// Empty arrays are all44AEco61B0e443F53< [] ;divclass="Name">< []>

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.