Skip to content

Commit 893b9d6

Browse files
authored
Merge pull request #14510 from Automattic/vkarpov15/gh-14473
types(query): make `FilterQuery` props resolve to `any` for generics support
2 parents e35fc63 + 3dd6edb commit 893b9d6

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

test/types/queries.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
FilterQuery,
1313
UpdateQuery,
1414
UpdateQueryKnownOnly,
15-
ApplyBasicQueryCasting,
1615
QuerySelector,
1716
InferSchemaType,
1817
ProjectionFields,
@@ -325,7 +324,7 @@ function gh11964() {
325324
}
326325

327326
function gh14397() {
328-
type Condition<T> = ApplyBasicQueryCasting<T> | QuerySelector<ApplyBasicQueryCasting<T>>; // redefined here because it's not exported by mongoose
327+
type Condition<T> = T | QuerySelector<T>; // redefined here because it's not exported by mongoose
329328

330329
type WithId<T extends object> = T & { id: string };
331330

@@ -592,3 +591,24 @@ function mongooseQueryOptions() {
592591
populate: 'test'
593592
});
594593
}
594+
595+
function gh14473() {
596+
class AbstractSchema {
597+
_id: any;
598+
createdAt: Date;
599+
updatedAt: Date;
600+
deletedAt: Date;
601+
602+
constructor() {
603+
this._id = 4;
604+
this.createdAt = new Date();
605+
this.updatedAt = new Date();
606+
this.deletedAt = new Date();
607+
}
608+
}
609+
610+
const generateExists = <D extends AbstractSchema = AbstractSchema>() => {
611+
const query: FilterQuery<D> = { deletedAt: { $ne: null } };
612+
const query2: FilterQuery<D> = { deletedAt: { $lt: new Date() } };
613+
};
614+
}

types/query.d.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
declare module 'mongoose' {
22
import mongodb = require('mongodb');
33

4-
type StringQueryTypeCasting = string | RegExp;
5-
type ObjectIdQueryTypeCasting = Types.ObjectId | string;
6-
type UUIDQueryTypeCasting = Types.UUID | string;
7-
8-
type QueryTypeCasting<T> = T extends string
9-
? StringQueryTypeCasting
10-
: T extends Types.ObjectId
11-
? ObjectIdQueryTypeCasting
12-
: T extends Types.UUID
13-
? UUIDQueryTypeCasting
14-
: T | any;
15-
16-
export type ApplyBasicQueryCasting<T> = T | T[] | (T extends (infer U)[] ? QueryTypeCasting<U> : T);
17-
export type Condition<T> = ApplyBasicQueryCasting<QueryTypeCasting<T>> | QuerySelector<ApplyBasicQueryCasting<QueryTypeCasting<T>>>;
18-
19-
type _FilterQuery<T> = {
20-
[P in keyof T]?: Condition<T[P]>;
21-
} & RootQuerySelector<T>;
4+
export type Condition<T> = T | QuerySelector<T | any> | any;
225

236
/**
247
* Filter query to select the documents that match the query
@@ -27,7 +10,9 @@ declare module 'mongoose' {
2710
* { age: { $gte: 30 } }
2811
* ```
2912
*/
30-
type FilterQuery<T> = _FilterQuery<T>;
13+
type FilterQuery<T> = {
14+
[P in keyof T]?: Condition<T[P]>;
15+
} & RootQuerySelector<T>;
3116

3217
type MongooseBaseQueryOptionKeys =
3318
| 'context'

0 commit comments

Comments
 (0)