DEV Community

Discussion on: The Strictest TypeScript Config

Collapse
 
what1s1ove profile image
Vladyslav Zubko • • Edited

Hey @artxe2 !

I think the issue lies more in the for...of/for...in loop, as it returns a string for a key variable. If you typecast it, everything will be fine 🙂

for (let key of Object.keys(obj)) {
  const typeKey = key as keyof typeof obj;

  obj[typeKey].func(); // ✅


  if (obj[typeKey]) {
    obj[typeKey].func(); // ✅
  }
}

for (let key in obj) {
  const typeKey = key as keyof typeof obj;

  obj[typeKey].func(); // ✅


  if (obj[typeKey]) {
    obj[typeKey].func(); // ✅
  }
}
Enter fullscreen mode Exit fullscreen mode