From a061ccdf71e1fc24f716d4ce791281d6168cf639 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 4 Nov 2022 18:10:11 -0400 Subject: [PATCH 1/2] fix(types): correct handling for model Fix #12573 --- test/types/models.test.ts | 6 ++++++ types/index.d.ts | 1 - types/inferschematype.d.ts | 2 +- types/utility.d.ts | 1 + 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/test/types/models.test.ts b/test/types/models.test.ts index c6678a5a0d1..153d86cbb4c 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -561,3 +561,9 @@ function findWithId() { TestModel.find(id); TestModel.findOne(id); } + +function gh12573ModelAny() { + const TestModel = model('Test', new Schema({})); + const doc = new TestModel(); + expectType(doc); +} 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 : From f9fb6123ab185d935e947d9637a0b9b1209e1052 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 6 Nov 2022 20:23:49 -0500 Subject: [PATCH 2/2] Update test/types/models.test.ts Co-authored-by: hasezoey --- test/types/models.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/types/models.test.ts b/test/types/models.test.ts index 153d86cbb4c..d31a447eee9 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -566,4 +566,6 @@ function gh12573ModelAny() { const TestModel = model('Test', new Schema({})); const doc = new TestModel(); expectType(doc); + const { fieldA } = doc; + expectType(fieldA); }