TS2488

error TS2488: Type ‘{ [month: number]: string; }‘ must have a ‘[Symbol.iterator]()‘ method that returns an iterator.

Solution

You have to add a property called [Symbol.iterator] to your object. The value of this property has to return an iterator. Here you can learn how to create an iterator.

Alternative Solution

If you run into this problem because of a for-of loop, then you can mitigate the problem by using the forEach() method of arrays:

Broken Code ❌

1
2
3
4
5
6
7
8
9
10
const months: {
[month: number]: string;
} = {
1: 'January',
2: 'February',
};

for (const month of months) {
console.log(month);
}

Fixed Code ✔️

1
2
3
4
5
6
7
8
9
10
const months: {
[month: number]: string;
} = {
1: 'January',
2: 'February',
};

Object.entries(months).forEach(month => {
console.log(month);
});