Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
BREAKING CHANGE: make FilterQuery properties no longer resolve to `an…
…y` in TypeScript
  • Loading branch information
vkarpov15 committed May 13, 2025
commit c66b840baf39ad1c822160bfc7506a8aa2a4b196
1 change: 1 addition & 0 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4452,6 +4452,7 @@ describe('Query', function() {
assert.strictEqual(deletedTarget?.name, targetName);

const target = await Person.find({}).findById(_id);

assert.strictEqual(target, null);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/types/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ function gh14473() {

const generateExists = <D extends AbstractSchema = AbstractSchema>() => {
const query: FilterQuery<D> = { deletedAt: { $ne: null } };
const query2: FilterQuery<D> = { deletedAt: { $lt: new Date() } };
const query2: FilterQuery<D> = { deletedAt: { $lt: new Date() } } as FilterQuery<D>;
};
}

Expand Down
26 changes: 20 additions & 6 deletions types/query.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
declare module 'mongoose' {
import mongodb = require('mongodb');

export type Condition<T> = T | QuerySelector<T | any> | any;
type StringQueryTypeCasting = string | RegExp;
type ObjectIdQueryTypeCasting = Types.ObjectId | string;
type UUIDQueryTypeCasting = Types.UUID | string;
type BufferQueryCasting = Buffer | mongodb.Binary | number[] | string | { $binary: string | mongodb.Binary };
type QueryTypeCasting<T> = T extends string
? StringQueryTypeCasting
: T extends Types.ObjectId
? ObjectIdQueryTypeCasting
: T extends Types.UUID
? UUIDQueryTypeCasting
: T extends Buffer
? BufferQueryCasting
: T;

export type ApplyBasicQueryCasting<T> = T | T[] | (T extends (infer U)[] ? QueryTypeCasting<U> : T);

export type Condition<T> = ApplyBasicQueryCasting<QueryTypeCasting<T>> | QuerySelector<ApplyBasicQueryCasting<QueryTypeCasting<T>>>;

/**
* Filter query to select the documents that match the query
Expand All @@ -12,9 +28,7 @@ declare module 'mongoose' {
*/
type RootFilterQuery<T> = FilterQuery<T> | Query<any, any> | Types.ObjectId;

type FilterQuery<T> = {
[P in keyof T]?: Condition<T[P]>;
} & RootQuerySelector<T> & { _id?: Condition<string>; };
type FilterQuery<T> = { [P in keyof T]?: Condition<T[P]>; } & RootQuerySelector<T>;

type MongooseBaseQueryOptionKeys =
| 'context'
Expand Down Expand Up @@ -58,13 +72,13 @@ declare module 'mongoose' {

type QuerySelector<T> = {
// Comparison
$eq?: T;
$eq?: T | null | undefined;
$gt?: T;
$gte?: T;
$in?: [T] extends AnyArray<any> ? Unpacked<T>[] : T[];
$lt?: T;
$lte?: T;
$ne?: T;
$ne?: T | null | undefined;
$nin?: [T] extends AnyArray<any> ? Unpacked<T>[] : T[];
// Logical
$not?: T extends string ? QuerySelector<T> | RegExp : QuerySelector<T>;
Expand Down