diff --git a/test/types/models.test.ts b/test/types/models.test.ts index c6678a5a0d1..d31a447eee9 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -561,3 +561,11 @@ function findWithId() { TestModel.find(id); TestModel.findOne(id); } + +function gh12573ModelAny() { + const TestModel = model('Test', new Schema({})); + const doc = new TestModel(); + expectType(doc); + const { fieldA } = doc; + expectType(fieldA); +} diff --git a/types/index.d.ts b/types/index.d.ts index 97663a46d84..f7cf20b31ac 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -551,7 +551,6 @@ declare module 'mongoose' { export type SchemaDefinitionType = T extends Document ? Omit> : T; // Helpers to simplify checks - type IfAny = 0 extends (1 & IFTYPE) ? THENTYPE : ELSETYPE; type IfUnknown = unknown extends IFTYPE ? THENTYPE : IFTYPE; // tests for these two types are located in test/types/lean.test.ts diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 27090b89eb4..3874e98b8a6 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 = ObtainSchemaGeneric; + type InferSchemaType = IfAny>; /** * @summary Obtains schema Generic type by using generic alias. diff --git a/types/utility.d.ts b/types/utility.d.ts index bfc531aa8f2..e5567b37b28 100644 --- a/types/utility.d.ts +++ b/types/utility.d.ts @@ -1,4 +1,5 @@ declare module 'mongoose' { + type IfAny = 0 extends (1 & IFTYPE) ? THENTYPE : ELSETYPE; type Unpacked = T extends (infer U)[] ? U :