From b8a4d43d975aed366d8d10a312c9a8dec8dce1b0 Mon Sep 17 00:00:00 2001 From: Carlos Ingles Date: Fri, 18 Nov 2022 16:53:29 +1100 Subject: [PATCH 1/3] feat: move TVirtuals to check if EnforcedVirtuals were supplied - enables virtual types on all model methods --- test/types/virtuals.test.ts | 8 ++++++++ types/models.d.ts | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/test/types/virtuals.test.ts b/test/types/virtuals.test.ts index a5b07f54419..e2a59f989ea 100644 --- a/test/types/virtuals.test.ts +++ b/test/types/virtuals.test.ts @@ -118,5 +118,13 @@ function autoTypedVirtuals() { const doc = new TestModel(); expectType(doc.domain); + TestModel.findOne({}).then((doc) => { + if (doc) { + expectType(doc.domain); + const json = doc.toJSON(); + expectType(json.domain); + } + }); + expectType>({} as InferredDocType); } diff --git a/types/models.d.ts b/types/models.d.ts index de5dad33b91..e4894f72eda 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -124,13 +124,13 @@ declare module 'mongoose' { interface RemoveOptions extends SessionOption, Omit {} const Model: Model; - interface Model extends + interface Model, EnforcedVirtuals> = IfEquals, EnforcedVirtuals>> extends NodeJS.EventEmitter, AcceptsDiscriminator, IndexManager, SessionStarter { - new (doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument, TVirtuals>> & ObtainSchemaGeneric; + new (doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument & ObtainSchemaGeneric; aggregate(pipeline?: PipelineStage[], options?: mongodb.AggregateOptions, callback?: Callback): Aggregate>; aggregate(pipeline: PipelineStage[], callback?: Callback): Aggregate>; From 8f17b8fb1fd8f88444ed1a72eb8cf857b746338d Mon Sep 17 00:00:00 2001 From: Carlos Ingles Date: Fri, 18 Nov 2022 16:53:46 +1100 Subject: [PATCH 2/3] feat: add virtuals to InferSchemaType generic --- test/types/virtuals.test.ts | 5 +++++ types/inferschematype.d.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/test/types/virtuals.test.ts b/test/types/virtuals.test.ts index e2a59f989ea..62f2083c8c5 100644 --- a/test/types/virtuals.test.ts +++ b/test/types/virtuals.test.ts @@ -126,5 +126,10 @@ function autoTypedVirtuals() { } }); + type TestDoc = InferSchemaType; + + const testDoc = { email: 'some email' } as TestDoc; + expectType(testDoc.domain); + expectType>({} as InferredDocType); } diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index e8c345fc707..9b7b98f6c54 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -38,7 +38,7 @@ declare module 'mongoose' { * // result * type UserType = {userName?: string} */ - type InferSchemaType = IfAny>; + type InferSchemaType = IfAny & ObtainSchemaGeneric>; /** * @summary Obtains schema Generic type by using generic alias. From a2df1182bf1c0dfd9c3b9a5c075887382f33bc03 Mon Sep 17 00:00:00 2001 From: Carlos Ingles Date: Sat, 19 Nov 2022 07:04:24 +1100 Subject: [PATCH 3/3] fix(test): change test to check for lack of virtuals - virtuals themselves do not have virtuals available --- test/types/virtuals.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/types/virtuals.test.ts b/test/types/virtuals.test.ts index 62f2083c8c5..1465b31e31b 100644 --- a/test/types/virtuals.test.ts +++ b/test/types/virtuals.test.ts @@ -89,7 +89,7 @@ function gh11543() { function autoTypedVirtuals() { type AutoTypedSchemaType = InferSchemaType; - type VirtualsType = { domain: string }; + type SchemaWithoutVirtual = Omit; type InferredDocType = FlatRecord>; const testSchema = new Schema({ @@ -101,11 +101,11 @@ function autoTypedVirtuals() { virtuals: { domain: { get() { - expectType & AutoTypedSchemaType>(this); + expectType>(this); return this.email.slice(this.email.indexOf('@') + 1); }, set() { - expectType & AutoTypedSchemaType>(this); + expectType>(this); }, options: {} } @@ -131,5 +131,5 @@ function autoTypedVirtuals() { const testDoc = { email: 'some email' } as TestDoc; expectType(testDoc.domain); - expectType>({} as InferredDocType); + expectType>({} as InferredDocType); }