TypeScript · TS0015
Do 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.
typescript/boundaries/no-inherited-key-lookupsrc/rule-packs/typescript/rules/no-inherited-key-lookup.ts Upstream docs Run this rule
pnpm vite-doctor . --extends auto,typescript/strict --rules typescript/boundaries/no-inherited-key-lookup
Why it matters
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.
Recommended fix
Check Object.hasOwn(record, key) before trusting the key, or keep the entries in a Map or an Object.create(null) record.
Useful links
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
}