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.
Provides functionality common to all JavaScript objects.
Object.hasOwn(
const object:any
object,
const key:string
key)) {
Error ts(2550) ― Property 'hasOwn' does not exist on type 'ObjectConstructor'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later.
const doSomething:any
doSomething(
const key:string
key);
}
}
for (const
const key:string
keyin
const object:any
object) {
if (!
var Object:ObjectConstructor
Provides functionality common to all JavaScript objects.
Object.hasOwn(
const object:any
object,
const key:string
key)) {
Error ts(2550) ― Property 'hasOwn' does not exist on type 'ObjectConstructor'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later.
continue;
}
const doSomething:any
doSomething(
const key:string
key);
}
for (const
const key:string
keyin
const object:any
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.
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).