Collections rules
no-element-overwrite
Section titled “no-element-overwrite”Reports a statement that writes the same index or key of a collection as an earlier statement of an unbroken run of writes, such as items[key] = value or map.set(key, value).
On in the recommended and all configs.
The second write replaces the first before anything reads it, which usually means one key is wrong.
What to do
Section titled “What to do”Correct the key that should differ, or delete the write whose value is lost.
Example
Section titled “Example”labels[0] = "first"; labels[0] = "second"; reports overwrite at the second write.
no-empty-collection
Section titled “no-empty-collection”Reports a read of a local array, map, or set that every write leaves empty.
On in the recommended and all configs.
A collection that nothing fills stays empty, so each read of it returns nothing and the code around it does no work.
What to do
Section titled “What to do”Fill the collection before the reads, or remove the collection and the code that reads it.
Example
Section titled “Example”const names = []; for (const name of names) print(name); reports empty at names in the loop.
no-in-misuse
Section titled “no-in-misuse”Reports in with an array on its right side, when the left side is not a number and not a quoted array method name.
On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.
in tests indexes and property names, not values, so "b" in ["a", "b"] is false.
What to do
Section titled “What to do”Use includes to test for a value: items.includes(value).
Example
Section titled “Example”if (name in names) reports misuse. Write if (names.includes(name)).
no-positive-index-check
Section titled “no-positive-index-check”Reports array.indexOf(value) > 0 on an array.
On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.
indexOf returns 0 for the first element, so > 0 misses a value at the start of the array.
What to do
Section titled “What to do”Use array.includes(value), or compare with >= 0 or !== -1.
Example
Section titled “Example”if (roles.indexOf(role) > 0) reports positive. Write if (roles.includes(role)).
no-unused-collection
Section titled “no-unused-collection”Reports a local array, map, or set that the code only writes, such as through push, set, or add, and never reads.
On in the recommended and all configs.
Filling a collection that nothing reads costs time and memory and often means that the code forgot to use the result.
What to do
Section titled “What to do”Read the collection where its contents matter, or remove it and the writes to it.
Example
Section titled “Example”const names = []; names.push(name); with no other use reports unused at names.
no-void-array-callback
Section titled “no-void-array-callback”Reports a callback whose every call signature returns void, passed to an array method that uses its result, such as map, filter, or reduce, or as the second argument of Array.from.
On in the recommended and all configs. It reports nothing without the type information of a TypeScript project.
These methods use the value that the callback returns, so a callback that returns nothing makes them produce undefined values, empty results, or an unchanged order.
What to do
Section titled “What to do”Pass a callback that returns the value the method needs, or call forEach when only the side effects matter.
Example
Section titled “Example”declare function log(value: number): void; [1, 2].map(log); reports void at log.
require-reduce-initial-value
Section titled “require-reduce-initial-value”Reports reduce called on an array with a callback and no initial value. Without type information, it reads only array literals and variables that hold one.
On in the recommended and all configs.
reduce without an initial value throws on an empty array, and uses the first element as the start value, which may have the wrong type.
What to do
Section titled “What to do”Pass the start value as the second argument, such as items.reduce((sum, item) => sum + item, 0).
Example
Section titled “Example”prices.reduce((sum, price) => sum + price) reports initial. Add , 0 as the second argument.