TypeScript · Boundaries

TS0015Do not look up runtime keys in plain-object records

Report `key in record` and `record[key]` reads where the record is a plain object and the key is runtime data, so Object.prototype keys such as toString and constructor match.

Why it happens

A plain object inherits Object.prototype. When the key comes from config, a request, a file name, or another record, "toString" in record is true and record"constructor" returns a function, so the code accepts a name nobody configured or calls the wrong value. This rule reports membership tests and reads on values typed Record<string, T>, a string index signature, or object, and on object-literal lookup tables. It skips known keys, null-prototype records, lookups protected by a matching own-key guard, and uses that treat inherited entries like missing entries.

Fix

Check Object.hasOwn(record, key) before trusting the key, or keep the entries in a Map or an Object.create(null) record.

Example

Check own keys before using a configured name

Before

export function useStore(stores: Record<string, Store>, name: string): Store {
  const store = stores[name]
  if (!store) throw new Error(`Unknown store: ${name}`)
  return store
}

After

export function useStore(stores: Record<string, Store>, name: string): Store {
  const store = Object.hasOwn(stores, name) ? stores[name] : undefined
  if (!store) throw new Error(`Unknown store: ${name}`)
  return store
}

Verify the fix

Run only this rule after editing so the report stays focused on the diagnostic you are closing:

pnpm vite-doctor . --extends auto,typescript/strict --rules typescript/boundaries/no-inherited-key-lookup
Copyright © 2026