Skip to content

no-in-operator-on-built-in-collections

Do not use the in operator to test entries in built-in Map and Set collections.

Why

The in operator checks properties on the collection object and its prototype, while .has() checks stored entries. The similar spelling can silently test the wrong namespace.

Fix

Use collection.has(key) for entry membership. Use Reflect.has(collection, property) when prototype-aware property lookup or Proxy has behavior is intentional.

Examples

Before — flagged Use Map.has for entry membership
src/cache.ts
declare const cache: Map<string, object>;
declare const key: string;
if (key in cache) use(cache);
After — preferred Preserve ordinary object property narrowing
src/cache.ts
declare const value: { id: string } | { slug: string };
if ("id" in value) use(value.id);