From 3dd6edb0b441e9a705eb77fd9a8fb3df575f0af4 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 8 Apr 2024 10:17:14 -0400 Subject: [PATCH] types(query): make `FilterQuery` props resolve to `any` for generics support Fix #14473 Re: #14459 --- test/types/queries.test.ts | 24 ++++++++++++++++++++++-- types/query.d.ts | 23 ++++------------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/test/types/queries.test.ts b/test/types/queries.test.ts index 3ebcaa861d0..d0af45a42a5 100644 --- a/test/types/queries.test.ts +++ b/test/types/queries.test.ts @@ -12,7 +12,6 @@ import { FilterQuery, UpdateQuery, UpdateQueryKnownOnly, - ApplyBasicQueryCasting, QuerySelector, InferSchemaType, ProjectionFields, @@ -325,7 +324,7 @@ function gh11964() { } function gh14397() { - type Condition = ApplyBasicQueryCasting | QuerySelector>; // redefined here because it's not exported by mongoose + type Condition = T | QuerySelector; // redefined here because it's not exported by mongoose type WithId = T & { id: string }; @@ -592,3 +591,24 @@ function mongooseQueryOptions() { populate: 'test' }); } + +function gh14473() { + class AbstractSchema { + _id: any; + createdAt: Date; + updatedAt: Date; + deletedAt: Date; + + constructor() { + this._id = 4; + this.createdAt = new Date(); + this.updatedAt = new Date(); + this.deletedAt = new Date(); + } + } + + const generateExists = () => { + const query: FilterQuery = { deletedAt: { $ne: null } }; + const query2: FilterQuery = { deletedAt: { $lt: new Date() } }; + }; +} diff --git a/types/query.d.ts b/types/query.d.ts index 32a502ed5a0..1e97ad6331d 100644 --- a/types/query.d.ts +++ b/types/query.d.ts @@ -1,24 +1,7 @@ declare module 'mongoose' { import mongodb = require('mongodb'); - type StringQueryTypeCasting = string | RegExp; - type ObjectIdQueryTypeCasting = Types.ObjectId | string; - type UUIDQueryTypeCasting = Types.UUID | string; - - type QueryTypeCasting = T extends string - ? StringQueryTypeCasting - : T extends Types.ObjectId - ? ObjectIdQueryTypeCasting - : T extends Types.UUID - ? UUIDQueryTypeCasting - : T | any; - - export type ApplyBasicQueryCasting = T | T[] | (T extends (infer U)[] ? QueryTypeCasting : T); - export type Condition = ApplyBasicQueryCasting> | QuerySelector>>; - - type _FilterQuery = { - [P in keyof T]?: Condition; - } & RootQuerySelector; + export type Condition = T | QuerySelector | any; /** * Filter query to select the documents that match the query @@ -27,7 +10,9 @@ declare module 'mongoose' { * { age: { $gte: 30 } } * ``` */ - type FilterQuery = _FilterQuery; + type FilterQuery = { + [P in keyof T]?: Condition; + } & RootQuerySelector; type MongooseBaseQueryOptionKeys = | 'context'