Skip to content

Commit d8b55b4

Browse files
committed
fix: cursor returns generic doc
1 parent 8c5f29f commit d8b55b4

File tree

5 files changed

+13
-14
lines changed

5 files changed

+13
-14
lines changed

src/cursor/aggregation_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
152152
* );
153153
* ```
154154
*/
155-
project<T = TSchema>($project: Document): AggregationCursor<T>;
155+
project<T = Document>($project: Document): AggregationCursor<T>;
156156
project($project: Document): this {
157157
assertUninitialized(this);
158158
this[kPipeline].push({ $project });

src/cursor/find_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ export class FindCursor<TSchema = Document> extends AbstractCursor<TSchema> {
361361
* );
362362
* ```
363363
*/
364-
project<T = TSchema>(value: Document): FindCursor<T>;
364+
project<T = Document>(value: Document): FindCursor<T>;
365365
project(value: Document): this {
366366
assertUninitialized(this);
367367
this[kBuiltOptions].projection = value;

src/operations/find.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import { isSharded } from '../cmap/wire_protocol/shared';
1616
import { ReadConcern } from '../read_concern';
1717
import type { ClientSession } from '../sessions';
1818

19-
/** @public */
19+
/**
20+
* @public
21+
* @typeParam TSchema - Unused schema definition, deprecated usage, only specify `FindOptions` with no generic
22+
*/
2023
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2124
export interface FindOptions<TSchema extends Document = Document> extends CommandOperationOptions {
2225
/** Sets the limit of documents returned in the query. */

test/types/community/collection/findX.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ const optionsWithProjection: FindOptions = {
128128
}
129129
};
130130

131-
expectNotType<FindOptions>({
131+
// this is changed in NODE-3454 to be the opposite test since Projection is flexible now
132+
expectAssignable<FindOptions>({
132133
projection: {
133134
make: 'invalid'
134135
}

test/types/community/cursor.test-d.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const cursor = collection
2222
.min({ age: 18 })
2323
.maxAwaitTimeMS(1)
2424
.maxTimeMS(1)
25-
.project({})
25+
// .project({}) -> projections removes the types from the returned documents
2626
.returnKey(true)
2727
.showRecordId(true)
2828
.skip(1)
@@ -31,6 +31,7 @@ const cursor = collection
3131

3232
expectType<FindCursor<{ foo: number }>>(cursor);
3333
expectType<Readable>(cursor.stream());
34+
expectType<FindCursor<Document>>(cursor.project({}));
3435

3536
collection.find().project({});
3637
collection.find().project({ notExistingField: 1 });
@@ -118,7 +119,7 @@ interface PublicMeme {
118119
likes: number;
119120
someRandomProp: boolean; // Projection makes no enforcement on anything
120121
// the convenience parameter project<X>() allows you to define a return type,
121-
// otherwise projections returns your schema
122+
// otherwise projections returns generic document
122123
}
123124

124125
const publicMemeProjection = {
@@ -132,18 +133,14 @@ const memeCollection = new Db(new MongoClient(''), '').collection<InternalMeme>(
132133
expectType<PublicMeme[]>(
133134
await memeCollection
134135
.find({ _id: { $in: [] } })
135-
.sort({ _id: -1 })
136-
.limit(3)
137136
.project<PublicMeme>(publicMemeProjection) // <== Argument of type T is not assignable to parameter of type U
138137
.toArray()
139138
);
140139

141-
// Returns you're untouched schema when no override given
142-
expectType<InternalMeme[]>(
140+
// Returns generic document when no override given
141+
expectNotType<InternalMeme[]>(
143142
await memeCollection
144143
.find({ _id: { $in: [] } })
145-
.sort({ _id: -1 })
146-
.limit(3)
147144
.project(publicMemeProjection) // <== Argument of type T is not assignable to parameter of type U
148145
.toArray()
149146
);
@@ -153,8 +150,6 @@ expectType<Document[]>(
153150
await new Db(new MongoClient(''), '')
154151
.collection('memes')
155152
.find({ _id: { $in: [] } })
156-
.sort({ _id: -1 })
157-
.limit(3)
158153
.project(publicMemeProjection) // <== Argument of type T is not assignable to parameter of type U
159154
.toArray()
160155
);

0 commit comments

Comments
 (0)