Skip to content

Commit

Permalink
types(querycursor): correct cursor async iterator type with `populate…
Browse files Browse the repository at this point in the history
…()` support

Fix #14374
  • Loading branch information
vkarpov15 committed Feb 26, 2024
1 parent 31e0a69 commit 00314a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 27 additions & 0 deletions test/types/querycursor.test.ts
Expand Up @@ -20,3 +20,30 @@ Test.find().cursor().
expectType<number>(i);
}).
then(() => console.log('Done!'));

async function gh14374() {
// `Parent` represents the object as it is stored in MongoDB
interface Parent {
child?: Types.ObjectId
name?: string
}
const ParentModel = model<Parent>(
'Parent',
new Schema({
child: { type: Schema.Types.ObjectId, ref: 'Child' },

Check warning on line 33 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 8

Check failure on line 33 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 8

Check warning on line 33 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 8

Check failure on line 33 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 8
name: String

Check warning on line 34 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 8

Check failure on line 34 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 8

Check warning on line 34 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 8

Check failure on line 34 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 6 spaces but found 8
})
)

Check failure on line 36 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Missing semicolon

Check failure on line 36 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Missing semicolon

Check failure on line 36 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Missing semicolon

Check failure on line 36 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Missing semicolon

interface Child {
name: string
}
const childSchema: Schema = new Schema({ name: String });

// This does not
const cursor = ParentModel.find({}).populate<{ child: Child }>('child').cursor()

Check failure on line 44 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Missing semicolon

Check failure on line 44 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Missing semicolon

Check failure on line 44 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Missing semicolon

Check failure on line 44 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Missing semicolon
for await (const doc of cursor) {
const _t: string = doc.child.name;

Check warning on line 46 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 4 spaces but found 6

Check failure on line 46 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 4 spaces but found 6

Check warning on line 46 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 4 spaces but found 6

Check failure on line 46 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Expected indentation of 4 spaces but found 6
}

}

Check warning on line 49 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Newline required at end of file but not found

Check warning on line 49 in test/types/querycursor.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Newline required at end of file but not found
4 changes: 2 additions & 2 deletions types/query.d.ts
Expand Up @@ -208,7 +208,7 @@ declare module 'mongoose' {
* A QueryCursor exposes a Streams3 interface, as well as a `.next()` function.
* This is equivalent to calling `.cursor()` with no arguments.
*/
[Symbol.asyncIterator](): AsyncIterableIterator<DocType>;
[Symbol.asyncIterator](): AsyncIterableIterator<Unpacked<ResultType>>;

/** Executes the query */
exec(): Promise<ResultType>;
Expand Down Expand Up @@ -286,7 +286,7 @@ declare module 'mongoose' {
* Returns a wrapper around a [mongodb driver cursor](https://mongodb.github.io/node-mongodb-native/4.9/classes/FindCursor.html).
* A QueryCursor exposes a Streams3 interface, as well as a `.next()` function.
*/
cursor(options?: QueryOptions<DocType>): Cursor<DocType, QueryOptions<DocType>>;
cursor(options?: QueryOptions<DocType>): Cursor<Unpacked<ResultType>, QueryOptions<DocType>>;

/**
* Declare and/or execute this query as a `deleteMany()` operation. Works like
Expand Down

0 comments on commit 00314a0

Please sign in to comment.