Skip to content

no-object-dictionary ​

A plain object used as a dictionary keyed by data: counts[word], delete cache[key], key in table.

Why: an object already has keys before you add any. counts["constructor"] is the Object function, "toString" in counts is true, and a "__proto__" key from user input changes the prototype. Adding and deleting many keys also turns the object into a slow dictionary (v8.dev), and keys an attacker chooses can flood its hash table (v8.dev). A Map has none of these problems.

ts
export function wordCounts(words: readonly string[]): Map<string, number> {
  const counts: Record<string, number> = {};
  const result = new Map<string, number>();
  for (const word of words) result.set(word, (counts[word] ?? 0) + 1);
  return result;
}
✓ Accepted0 diagnostics
ts
export function wordCounts(words: readonly string[]): Map<string, number> {
  const counts = new Map<string, number>();
  for (const word of words) counts.set(word, (counts.get(word) ?? 0) + 1);
  return counts;
}

The rule applies to bindings typed Record<string, V> or { [key: string]: V } (a parameter's Readonly<...> view of one too: reading constructor from it is the same hazard), and to ones made with {} without a type. Record<"a" | "b", V> has a fixed set of keys and is fine, and so is {} declared with such a type (const counts: Partial<Record<Size, number>> = {}). To read one key safely from a dictionary, use getOwn(object, key) from @evobug_com/safe-ts-std/dict.

A key whose type is a union of string literals is fine too, when none of them is a name Object.prototype has (constructor, toString, __proto__, ...): prices[size] with size: Size and type Size = "s" | "m" | "l" (declared here or imported), size: "s" | "m", and the variable of a for...of over such a list or over an array literal of strings (for (const size of SIZES) with const SIZES = ["s", "m"] as const):

✓ Accepted0 diagnostics
ts
type Size = "s" | "m" | "l";
const SIZES = ["s", "m", "l"] as const;

export function stockOf(size: Size, stock: Record<string, number>): number {
  return stock[size] ?? 0;
}

export function totalStock(stock: Record<string, number>): number {
  let total = 0;
  for (const size of SIZES) total += stock[size] ?? 0;
  return total;
}

What the rule sees: bindings, by how they are declared. A dictionary reached through a field (shop.prices[name]) isn't checked, so const prices: Record<string, number> = shop.prices makes the same read reported: give the field a Map or a closed key type to make both agree.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra