From c2c13514bfbf8b1e422d8c300111c14b540c57f8 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 16 Sep 2024 17:08:08 -0400 Subject: [PATCH 1/3] types: make __v a number, only set __v on top-level documents Re: #12959 --- test/types/document.test.ts | 16 ++++++++++++++++ types/document.d.ts | 3 --- types/index.d.ts | 10 +++++++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/test/types/document.test.ts b/test/types/document.test.ts index 84451edf0f2..af458104125 100644 --- a/test/types/document.test.ts +++ b/test/types/document.test.ts @@ -359,3 +359,19 @@ function gh13738() { expectType(person.get('dob')); expectType<{ theme: string; alerts: { sms: boolean } }>(person.get('settings')); } + +async function gh12959() { + const subdocSchema = new Schema({ foo: { type: 'string', required: true } }); + + const schema = new Schema({ + subdocArray: { type: [subdocSchema], required: true }, + }); + + const Model = model('test', schema); + + const doc = await Model.findById('id').orFail(); + expectType(doc._id); + expectType(doc.__v); + + expectError(doc.subdocArray[0].__v); +} \ No newline at end of file diff --git a/types/document.d.ts b/types/document.d.ts index 5557269783f..7bc140461ee 100644 --- a/types/document.d.ts +++ b/types/document.d.ts @@ -24,9 +24,6 @@ declare module 'mongoose' { /** This documents _id. */ _id: T; - /** This documents __v. */ - __v?: any; - /** Assert that a given path or paths is populated. Throws an error if not populated. */ $assertPopulated(path: string | string[], values?: Partial): Omit & Paths; diff --git a/types/index.d.ts b/types/index.d.ts index 02c975a4eb3..3ec72ac281d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -138,6 +138,10 @@ declare module 'mongoose' { ? IfAny> : T & { _id: Types.ObjectId }; + export type Default__v = T extends { __v?: infer U } + ? T + : T & { __v?: number }; + /** Helper type for getting the hydrated document type from the raw document type. The hydrated document type is what `new MyModel()` returns. */ export type HydratedDocument< DocType, @@ -147,12 +151,12 @@ declare module 'mongoose' { DocType, any, TOverrides extends Record ? - Document & Require_id : + Document & Default__v> : IfAny< TOverrides, - Document & Require_id, + Document & Default__v>, Document & MergeType< - Require_id, + Default__v>, TOverrides > > From 11494c75516fe25e6914d9f687331f4f60a097a7 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 17 Sep 2024 11:29:00 -0400 Subject: [PATCH 2/3] Update test/types/document.test.ts Co-authored-by: hasezoey --- test/types/document.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/document.test.ts b/test/types/document.test.ts index af458104125..ddba9cfcdd0 100644 --- a/test/types/document.test.ts +++ b/test/types/document.test.ts @@ -364,7 +364,7 @@ async function gh12959() { const subdocSchema = new Schema({ foo: { type: 'string', required: true } }); const schema = new Schema({ - subdocArray: { type: [subdocSchema], required: true }, + subdocArray: { type: [subdocSchema], required: true } }); const Model = model('test', schema); From 05a22346479d6c2fad0cb0467a8b093243bbb22c Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 17 Sep 2024 11:29:11 -0400 Subject: [PATCH 3/3] Update test/types/document.test.ts Co-authored-by: hasezoey --- test/types/document.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/document.test.ts b/test/types/document.test.ts index ddba9cfcdd0..685410eafc4 100644 --- a/test/types/document.test.ts +++ b/test/types/document.test.ts @@ -374,4 +374,4 @@ async function gh12959() { expectType(doc.__v); expectError(doc.subdocArray[0].__v); -} \ No newline at end of file +}