Typescript error TS2504 when trying to use async iterators with Mongoose 5.11.14: must have a '[Symbol.asyncIterator]()' Typescript error TS2504 when trying to use async iterators with Mongoose 5.11.14: must have a '[Symbol.asyncIterator]()' mongoose mongoose

Typescript error TS2504 when trying to use async iterators with Mongoose 5.11.14: must have a '[Symbol.asyncIterator]()'


Yes, there is missing type information.

@types/mongoose@5.10.3 defines [Symbol.asyncIterator] on DocumentQuery, and Query extends DocumentQuery.

mongoose@5.11.14 does not define [Symbol.asyncIterator] on Query.

This method must be defined on any interface that is used on the right-hand side of a for await...of statement.

I do notice that both packages define a QueryCursor which extends stream.Readable. This class does define a [Symbol.asyncIterator] method, and the cursor() method of the Query interface returns an instance of QueryCursor. Try to see if the following compiles and runs as you expect without using @types/mongoose:

for await (const document of db.myModel.find().cursor()) {//                                            ^^^^^^^^^...}