for-in
loops should include an if
statement JS-0051137 const obj = {} as Record<keyof TOptions, unknown>;
138 const options = optionsCallback(optionsFactory);
139
140 for (const key in options) {141 obj[key] = {142 ...configuration?.[key],143 ...options[key],144 };145 }146
147 return obj as {
148 [key in keyof TOptions]: TOptions[key] & FieldConfiguration<TOptions>;
Looping over objects with a for in
loop will include properties that are inherited through the prototype chain.
This behavior can lead to unexpected keys in your for loop.
for (const key in foo) {
doSomething(key);
}
for (const key in foo) {
if (Object.prototype.hasOwnProperty.call(foo, key)) {
doSomething(key);
}
}
for (const key of Object.keys(foo)) {
doSomething(key);
}