Skip to content

Commit 7d70708

Browse files
authored
Merge pull request #15676 from Automattic/vkarpov15/gh-15671
types: avoid making FilterQuery a conditional type because of how typescript handles distributed conditional unions
2 parents daa978d + 616b7ca commit 7d70708

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

test/types/queries.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {
1+
import mongoose, {
22
Condition,
33
HydratedDocument,
44
Schema,
@@ -770,3 +770,31 @@ async function gh3230() {
770770

771771
console.log(await Test.findById(test._id).populate('arr.testRef', { name: 1, prop: 1, _id: 0, __t: 0 }));
772772
}
773+
774+
function gh15671() {
775+
interface DefaultQuery {
776+
search?: string;
777+
}
778+
779+
type QueryFeaturesProps = {
780+
params: Partial<DefaultQuery>;
781+
};
782+
783+
const queryFeatures = async <T, R, TQueryOp>(
784+
query: mongoose.Query<R, T, object, T, TQueryOp>,
785+
{ params }: QueryFeaturesProps
786+
): Promise<{ content: mongoose.GetLeanResultType<T, R, TQueryOp>; result?: number }> => {
787+
if (params.search) {
788+
query.find({
789+
$text: {
790+
$search: params.search
791+
}
792+
});
793+
}
794+
795+
const content = await query.lean().orFail().exec();
796+
return {
797+
content
798+
};
799+
};
800+
}

types/query.d.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ declare module 'mongoose' {
1010
* { age: { $gte: 30 } }
1111
* ```
1212
*/
13-
type RootFilterQuery<T> = FilterQuery<T> | Query<any, any> | Types.ObjectId;
13+
type RootFilterQuery<T> = IsItRecordAndNotAny<T> extends true ? FilterQuery<T> | Query<any, any> | Types.ObjectId : FilterQuery<Record<string, any>> | Query<any, any> | Types.ObjectId;
1414

15-
type FilterQuery<T> = IsItRecordAndNotAny<T> extends true ?
16-
({ [P in keyof T]?: Condition<T[P]>; } & RootQuerySelector<T> & { _id?: Condition<string>; }) :
17-
FilterQuery<Record<string, any>>;
15+
type FilterQuery<T> = ({ [P in keyof T]?: Condition<T[P]>; } & RootQuerySelector<T> & { _id?: Condition<string>; });
1816

1917
type MongooseBaseQueryOptionKeys =
2018
| 'context'
@@ -424,32 +422,17 @@ declare module 'mongoose' {
424422

425423
/** Creates a `find` query: gets a list of documents that match `filter`. */
426424
find(
427-
filter: RootFilterQuery<RawDocType>,
425+
filter?: RootFilterQuery<RawDocType>,
428426
projection?: ProjectionType<RawDocType> | null,
429427
options?: QueryOptions<RawDocType> | null
430428
): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find', TDocOverrides>;
431-
find(
432-
filter: RootFilterQuery<RawDocType>,
433-
projection?: ProjectionType<RawDocType> | null
434-
): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find', TDocOverrides>;
435-
find(
436-
filter: RootFilterQuery<RawDocType>
437-
): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find', TDocOverrides>;
438-
find(): QueryWithHelpers<Array<DocType>, DocType, THelpers, RawDocType, 'find', TDocOverrides>;
439429

440430
/** Declares the query a findOne operation. When executed, returns the first found document. */
441431
findOne(
442432
filter?: RootFilterQuery<RawDocType>,
443433
projection?: ProjectionType<RawDocType> | null,
444434
options?: QueryOptions<RawDocType> | null
445435
): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
446-
findOne(
447-
filter?: RootFilterQuery<RawDocType>,
448-
projection?: ProjectionType<RawDocType> | null
449-
): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
450-
findOne(
451-
filter?: RootFilterQuery<RawDocType>
452-
): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
453436

454437
/** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
455438
findOneAndDelete(
@@ -484,13 +467,6 @@ declare module 'mongoose' {
484467
projection?: ProjectionType<RawDocType> | null,
485468
options?: QueryOptions<RawDocType> | null
486469
): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
487-
findById(
488-
id: mongodb.ObjectId | any,
489-
projection?: ProjectionType<RawDocType> | null
490-
): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
491-
findById(
492-
id: mongodb.ObjectId | any
493-
): QueryWithHelpers<DocType | null, DocType, THelpers, RawDocType, 'findOne', TDocOverrides>;
494470

495471
/** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
496472
findByIdAndDelete(

0 commit comments

Comments
 (0)