Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript 2.8.3 Type must have a Symbol.iterator method that returns an iterator

Tags:

typescript

I am running into an error that says

Type must have a '[Symbol.iterator]()' method that returns an iterator. 

It hopes on the demarcated line:

class Test {     private async do() {         const done = [...(await this.test())]; // Here's the error     }      private async *test(): AsyncIterableIterator<string> {         return;     } } 

I have found a few issues in the TypeScript GitHub repository, but none seem to have helped. They all suggest adding new entries to lib. I am using es6 target and have added esnext, dom and es2018. This has had zero effect on the error.

Do I miss more lib entries (which I doubt as the ones I am using are the catchall ones with everything in) or is the code I am using invalid?

like image 495
Tomáš Hübelbauer Avatar asked May 08 '18 13:05

Tomáš Hübelbauer


1 Answers

this has helped me. in tsconfig, add this :

{     "compilerOptions": {         "lib": [             "es5", "es6", "dom", "dom.iterable"         ]     } } 

This is required for tsconfig to know which interfaces to import while transpiling your code.

dom.iterable includes all the interfaces mentioned here : https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.iterable.d.ts

Also thanks to @domlas for mentioning this. I already upvoted his comment so that it is treated as default reason.

like image 192
Rohit Gupta Avatar answered Nov 12 '22 00:11

Rohit Gupta