forInGuards
Reports for-in loops without an if statement to filter inherited properties.
✅ This rule is included in the ts preset.
Looping over objects with a for-in loop will include properties inherited through the prototype chain.
This can lead to unexpected items being iterated over when the object inherits from a non-standard prototype.
Examples
Section titled “Examples”for (const const key: string
key in const object: Record<string, unknown>
object) { const doSomething: (key: string) => void
doSomething(const key: string
key);}for (const const key: string
key in const object: Record<string, unknown>
object) { const doSomething: (key: string) => void
doSomething(const key: string
key);}for (const const key: string
key in const object: Record<string, unknown>
object) { if (const condition: boolean
condition) { const doSomething: (key: string) => void
doSomething(const key: string
key); } const doSomethingElse: (key: string) => void
doSomethingElse(const key: string
key);}for (const const key: string
key in const object: Record<string, unknown>
object) { if (var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.ObjectConstructor.hasOwn(o: object, v: PropertyKey): boolean
Determines whether an object has a property with the specified name.
hasOwn(const object: Record<string, unknown>
object, const key: string
key)) { const doSomething: (key: string) => void
doSomething(const key: string
key); }}for (const const key: string
key in const object: Record<string, unknown>
object) { if (!var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.ObjectConstructor.hasOwn(o: object, v: PropertyKey): boolean
Determines whether an object has a property with the specified name.
hasOwn(const object: Record<string, unknown>
object, const key: string
key)) { continue; } const doSomething: (key: string) => void
doSomething(const key: string
key);}for (const const key: string
key in const object: Record<string, unknown>
object) { if (var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.ObjectConstructor.prototype: Object
A reference to the prototype for a class of objects.
prototype.Object.hasOwnProperty(v: PropertyKey): boolean
Determines whether an object has a property with the specified name.
hasOwnProperty.CallableFunction.call<Record<string, unknown>, [string], boolean>(this: (this: Record<string, unknown>, args_0: string) => boolean, thisArg: Record<string, unknown>, args_0: string): boolean
Calls the function with the specified object as the this value and the specified rest arguments as the arguments.
call(const object: Record<string, unknown>
object, const key: string
key)) { const doSomething: (key: string) => void
doSomething(const key: string
key); }}for (const const key: string
key in const object: Record<string, unknown>
object);for (const const key: string
key in const object: Record<string, unknown>
object) {}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you are certain that the objects you iterate over do not have inherited properties from custom prototypes, you might choose to disable this rule.
This is common when iterating over plain objects created with object literals or Object.create(null).
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useGuardForIn - Deno:
guard-for-in - ESLint:
guard-for-in - Oxlint:
eslint/guard-for-in