# Type Object Must Have A Symbol.iterator Method That Returns An Iterator In TypeScript – How To Fix It?

In this article, we talk about the error message: Type object must have a Symbol.iterator method that returns an iterator in TypeScript. Why do we encounter this problem, and what is its solution? Let’;s go into detail now.

## Reason for error: Type object must have a Symbol.iterator method that returns an iterator in TypeScript

The reason why you have this problem is that you are using spread syntax(…) incorrectly.

We are sure that you tried to unpack an object in an array using spread syntax(…). As a result, you get the error message.

Here is a specific example of where an error occurrs in your program:

const myCar = { brand: 'Porsche', model: '911', seats: 2 };
const result = [...myCar];
console.log(result)

Output:

Type '{ brand: string; model: string; seats: number; }' must have a '[Symbol.iterator]()' method that returns an iterator.

You can see that the spread syntax(…) does not allow us to unpack an object straight into an array, so the error is understandable.

## Method to resolve the problem “Type object must have a Symbol.iterator method that returns an iterator” in TypeScript

There are many ways to fix this problem, depending on the situation that caused the error to occur on your machine. Take a look at some of the methods below:

### Wrap your object in an array

In case you want to decompress your object into an array, surround your object in an array. By using square brackets ([ ]) around the object, you will be able to use the spread syntax(…).

const myCar = { brand: 'Porsche', model: '911', seats: 2 };

//wrap your object into an array
const temp = [myCar]

//using square brackets
const result = [...temp];

console.log(result)

Output:

[{
"brand": "Porsche",
"model": "911",
"seats": 2
}]


### Wrap your object in another object

You might also consider wrapping all the objects in a single object if you want to combine multiple objects into one that stores all the information.

const myCar = { brand: 'Porsche', model: '911', seats: 2 };

//create new object
const characteristics = { color: ['white', 'blue'], state: 'new'}

//merge two objects together
const result = {...myCar, ...characteristics}

console.log(result)

Output:

[{
"brand": "Porsche",
"model": "911",
"seats": 2,
"color": [
"white",
"blue"
],
"state": "new"
}] 

With the above two approaches, you can safely use the spread syntax(…) to unpack an object into an array without worrying about errors.

## Summary

Hopefully, the above information has helped you have a clearer view of the error “Type object must have a Symbol.iterator method that returns an iterator” in TypeScript and how to fix it. Try practicing on your computer and see the results. Thank you for being so interested in our article.

Maybe you are interested: