Which fix removes the bug?
const prices: Record<string, number> = { tea: 2, jam: 5 };
for (const p of prices) {
console.log(p); // expected 2, then 5
}
- Use
for (const p in prices) - Type it as
number[]instead - Add
as constto the object - Loop over
Object.values(prices)
Plain objects are not iterable, so for...of throws. Object.values gives an array of the values; for...in would give the keys.
Play Problem Solving ▸