diff --git a/test/types/queries.test.ts b/test/types/queries.test.ts index 23ae0424cb..bb240dbc4c 100644 --- a/test/types/queries.test.ts +++ b/test/types/queries.test.ts @@ -38,11 +38,11 @@ const schema: Schema, {}, QueryHelpers> = new endDate: Date }); -schema.query._byName = function(name: string): QueryWithHelpers { +schema.query._byName = function (name: string): QueryWithHelpers { return this.find({ name }); }; -schema.query.byName = function(name: string): QueryWithHelpers { +schema.query.byName = function (name: string): QueryWithHelpers { expectError(this.notAQueryHelper()); return this._byName(name); }; @@ -156,12 +156,12 @@ const p1: Record = Test.find().projection('age docs.id'); const p2: Record | null = Test.find().projection(); const p3: null = Test.find().projection(null); -expectError(Test.find({ }, { name: 'ss' })); // Only 0 and 1 are allowed +expectError(Test.find({}, { name: 'ss' })); // Only 0 and 1 are allowed Test.find({}, { name: 3 }); Test.find({}, { name: true, age: false, endDate: true, tags: 1 }); Test.find({}, { name: true, age: false, endDate: true }); Test.find({}, { name: false, age: false, tags: false, child: { name: false }, docs: { myId: false, id: true } }); -expectError(Test.find({ }, { tags: { something: 1 } })); // array of strings or numbers should only be allowed to be a boolean or 1 and 0 +expectError(Test.find({}, { tags: { something: 1 } })); // array of strings or numbers should only be allowed to be a boolean or 1 and 0 Test.find({}, { name: true, age: true, endDate: true, tags: 1, child: { name: true }, docs: { myId: true, id: true } }); // This should be allowed Test.find({}, { name: 1, age: 1, endDate: 1, tags: 1, child: { name: 1 }, docs: { myId: 1, id: 1 } }); // This should be allowed Test.find({}, { _id: 0, name: 1, age: 1, endDate: 1, tags: 1, child: 1, docs: 1 }); // _id is an exception and should be allowed to be excluded @@ -378,7 +378,7 @@ function gh14397() { } function gh12091() { - interface IUser{ + interface IUser { friendsNames: string[]; } const userSchema = new Schema({ @@ -412,19 +412,19 @@ async function gh12342_manual() { interface ProjectQueryHelpers { byName(name: string): QueryWithHelpers< - HydratedDocument[], - HydratedDocument, - ProjectQueryHelpers + HydratedDocument[], + HydratedDocument, + ProjectQueryHelpers > } type ProjectModelType = Model; const ProjectSchema = new Schema< - Project, - Model, - {}, - ProjectQueryHelpers + Project, + Model, + {}, + ProjectQueryHelpers >({ name: String, stars: Number @@ -515,9 +515,9 @@ async function gh13142() { projection: Projection, options: Options ): Promise< - Options['lean'] extends true - ? Pick> | null - : HydratedDocument>> | null + Options['lean'] extends true + ? Pick> | null + : HydratedDocument>> | null > { return this.blogModel.findOne(filter, projection, options); } @@ -588,7 +588,7 @@ function gh14190() { ); expectAssignable< ModifyResult> - >(res); + >(res); const res2 = await UserModel.find().findByIdAndDelete( '0'.repeat(24), @@ -596,7 +596,7 @@ function gh14190() { ); expectAssignable< ModifyResult> - >(res2); + >(res2); } function mongooseQueryOptions() { @@ -717,7 +717,7 @@ function gh14510() { // From https://stackoverflow.com/questions/56505560/how-to-fix-ts2322-could-be-instantiated-with-a-different-subtype-of-constraint: // "Never assign a concrete type to a generic type parameter, consider it as read-only!" // This function is generally something you shouldn't do in TypeScript, can work around it with `as` though. - function findById(model: Model, _id: Types.ObjectId | string) { + function findById(model: Model, _id: Types.ObjectId | string) { return model.find({ _id: _id } as QueryFilter); } } @@ -863,5 +863,10 @@ async function gh15786() { } const schema = new Schema, {}, {}, {}, DocStatics>({}); - schema.static({ m1() {} } as DocStatics); + schema.static({ m1() { } } as DocStatics); +} + +function gh15854() { + const filter = { foo: 'hi' } as mongoose.QueryFilter<{ foo: string }>; + filter.foo; // Should not error: Property 'foo' does not exist on type '_QueryFilter<{ foo: string; }>' } diff --git a/types/query.d.ts b/types/query.d.ts index f0e3e86022..441df4a6c7 100644 --- a/types/query.d.ts +++ b/types/query.d.ts @@ -9,18 +9,18 @@ declare module 'mongoose' { type QueryTypeCasting = T extends string ? StringQueryTypeCasting : T extends Types.ObjectId - ? ObjectIdQueryTypeCasting - : T extends Types.UUID - ? UUIDQueryTypeCasting - : T extends Buffer - ? BufferQueryCasting - : T extends NativeDate - ? DateQueryTypeCasting - : T; + ? ObjectIdQueryTypeCasting + : T extends Types.UUID + ? UUIDQueryTypeCasting + : T extends Buffer + ? BufferQueryCasting + : T extends NativeDate + ? DateQueryTypeCasting + : T; export type ApplyBasicQueryCasting = QueryTypeCasting | QueryTypeCasting | (T extends (infer U)[] ? QueryTypeCasting : T) | null; - type _QueryFilter = ({ [P in keyof T]?: mongodb.Condition>; } & mongodb.RootFilterOperators<{ [P in keyof T]?: ApplyBasicQueryCasting; }>); + type _QueryFilter = ({ [P in keyof T]?: mongodb.Condition>; } & mongodb.RootFilterOperators<{ [P in keyof T]?: ApplyBasicQueryCasting; }> & Record); type QueryFilter = IsItRecordAndNotAny extends true ? _QueryFilter> : _QueryFilter>; type MongooseBaseQueryOptionKeys = @@ -177,14 +177,14 @@ declare module 'mongoose' { type MergePopulatePaths> = QueryOp extends QueryOpThatReturnsDocument ? ResultType extends null - ? ResultType - : ResultType extends (infer U)[] - ? U extends Document - ? HydratedDocument, TDocOverrides, TQueryHelpers>[] - : (MergeType)[] - : ResultType extends Document - ? HydratedDocument, TDocOverrides, TQueryHelpers> - : MergeType + ? ResultType + : ResultType extends (infer U)[] + ? U extends Document + ? HydratedDocument, TDocOverrides, TQueryHelpers>[] + : (MergeType)[] + : ResultType extends Document + ? HydratedDocument, TDocOverrides, TQueryHelpers> + : MergeType : MergeType; class Query> implements SessionOperation { @@ -350,8 +350,8 @@ declare module 'mongoose' { ): QueryWithHelpers< Array< DocKey extends keyof WithLevel1NestedPaths - ? WithoutUndefined[DocKey]>> - : ResultType + ? WithoutUndefined[DocKey]>> + : ResultType >, DocType, THelpers, @@ -366,8 +366,8 @@ declare module 'mongoose' { ): QueryWithHelpers< Array< DocKey extends keyof WithLevel1NestedPaths - ? WithoutUndefined[DocKey]>> - : ResultType + ? WithoutUndefined[DocKey]>> + : ResultType >, DocType, THelpers, @@ -561,62 +561,62 @@ declare module 'mongoose' { /** Sets the lean option. */ lean(): QueryWithHelpers< ResultType extends null - ? GetLeanResultType | null - : GetLeanResultType, + ? GetLeanResultType | null + : GetLeanResultType, DocType, THelpers, RawDocType, QueryOp, TDocOverrides - >; + >; lean( val: true | LeanOptions ): QueryWithHelpers< ResultType extends null - ? GetLeanResultType | null - : GetLeanResultType, + ? GetLeanResultType | null + : GetLeanResultType, DocType, THelpers, RawDocType, QueryOp, TDocOverrides - >; + >; lean( val: false ): QueryWithHelpers< ResultType extends AnyArray - ? DocType[] - : ResultType extends null - ? DocType | null - : DocType, + ? DocType[] + : ResultType extends null + ? DocType | null + : DocType, DocType, THelpers, RawDocType, QueryOp, TDocOverrides - >; + >; lean(): QueryWithHelpers< ResultType extends null - ? LeanResultType | null - : LeanResultType, + ? LeanResultType | null + : LeanResultType, DocType, THelpers, RawDocType, QueryOp, TDocOverrides - >; + >; lean( val: boolean | LeanOptions ): QueryWithHelpers< ResultType extends null - ? LeanResultType | null - : LeanResultType, + ? LeanResultType | null + : LeanResultType, DocType, THelpers, RawDocType, QueryOp, TDocOverrides - >; + >; /** Specifies the maximum number of documents the query will return. */ limit(val: number): this; @@ -797,12 +797,12 @@ declare module 'mongoose' { {}, ResultType, ResultType extends any[] - ? ResultType extends HydratedDocument[] - ? HydratedDocument[] - : RawDocTypeOverride[] - : (ResultType extends HydratedDocument - ? HydratedDocument - : RawDocTypeOverride) | (null extends ResultType ? null : never) + ? ResultType extends HydratedDocument[] + ? HydratedDocument[] + : RawDocTypeOverride[] + : (ResultType extends HydratedDocument + ? HydratedDocument + : RawDocTypeOverride) | (null extends ResultType ? null : never) >, DocType, THelpers,