HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptModerate

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
typescriptmusthavereturnsiteratormethodthattypesymbol

Problem

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 {
        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?

Solution

As I commented above, the spread operator is unfortunately currently unsupported for asynchronous iterators. The relevant issue in GitHub is tc39/proposal-async-iteration#103.

Recap from that issue (minus extraneous stuff such as "you could do it this way, oops no you can't, never mind"):

@jedwards1211 said:


Will array spread operator support ever be part of this proposal?

@domenic ansered:


Not part of this proposal... Another proposal could certainly contemplate something new here.

And I don't see a proposal elsewhere (want to start one?). In any case, since it isn't part of even JavaScript ESNext, it most likely won't get added to TypeScript.

The most viable alternative is the for await syntax detailed in @estus's answer. Sorry I couldn't be more helpful. Good luck!

Context

Stack Overflow Q#50234481, score: 35

Revisions (0)

No revisions yet.