diff --git a/index.d.ts b/index.d.ts index b7c11aebd85..a542e62f81a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1270,10 +1270,10 @@ declare module 'mongoose' { /** * Create a new schema */ - constructor(definition?: SchemaDefinition>, options?: SchemaOptions); + constructor(definition?: SchemaDefinition>, options?: SchemaOptions); /** Adds key path / schema type pairs to this schema. */ - add(obj: SchemaDefinition> | Schema, prefix?: string): this; + add(obj: SchemaDefinition> | Schema, prefix?: string): this; /** * Array of child schemas (from document arrays and single nested subdocs) @@ -2587,8 +2587,12 @@ declare module 'mongoose' { export type UpdateQuery = (_UpdateQuery> & mongodb.MatchKeysAndValues>); type _AllowStringsForIds = { - [K in keyof T]: [Extract] extends [never] ? T[K] : T[K] | string; - }; + [K in keyof T]: [Extract] extends [never] + ? T[K] extends TreatAsPrimitives + ? T[K] + : _AllowStringsForIds + : T[K] | string; + }; export type DocumentDefinition = _AllowStringsForIds>; type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined; diff --git a/test/typescript/queries.ts b/test/typescript/queries.ts index b832e212815..6f56f775558 100644 --- a/test/typescript/queries.ts +++ b/test/typescript/queries.ts @@ -8,7 +8,7 @@ interface QueryHelpers { const schema: Schema> = new Schema({ name: { type: 'String' }, tags: [String], - docs: [{ nested: { id: Number, tags: [String] } }], + docs: [{ _id: 'ObjectId', id: Number, tags: [String] }], endDate: Date }); @@ -22,6 +22,7 @@ schema.query.byName = function(name: string): QueryWithHelpers { }; interface ISubdoc extends Document { + myId?: Types.ObjectId; id?: number; tags?: string[]; } @@ -89,6 +90,8 @@ Test.findOneAndUpdate({ name: 'test' }, [{ $set: { endDate: true } }]); Test.findByIdAndUpdate({ name: 'test' }, { name: 'test2' }, (err, doc) => console.log(doc)); +Test.findOneAndUpdate({ name: 'test' }, { 'docs.0.myId': '0'.repeat(24) }); + const query: Query = Test.findOne(); query instanceof Query;