diff --git a/test/types/virtuals.test.ts b/test/types/virtuals.test.ts index a5b07f54419..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: {} } @@ -118,5 +118,18 @@ function autoTypedVirtuals() { const doc = new TestModel(); expectType(doc.domain); - expectType>({} as InferredDocType); + TestModel.findOne({}).then((doc) => { + if (doc) { + expectType(doc.domain); + const json = doc.toJSON(); + expectType(json.domain); + } + }); + + 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. 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>;