From 5409717805044aab850c387f12b4ba63e6ff6198 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 24 Jun 2022 16:44:52 -0400 Subject: [PATCH 1/2] fix(types): avoid adding non-existent properties from model constructor for typegoose Fix #11960 --- test/types/document.test.ts | 2 -- types/models.d.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/test/types/document.test.ts b/test/types/document.test.ts index 3319a4a59a6..dfb476cb55f 100644 --- a/test/types/document.test.ts +++ b/test/types/document.test.ts @@ -192,8 +192,6 @@ function autoTypedDocument() { expectType(AutoTypeModelInstance.userName); expectType(AutoTypeModelInstance.favoritDrink); expectType(AutoTypeModelInstance.favoritColorMode); - expectType(AutoTypeModelInstance.unExistProperty); - expectType(AutoTypeModelInstance.description); // Document-Methods-tests expectType>(new AutoTypedModel().instanceFn()); diff --git a/types/models.d.ts b/types/models.d.ts index f5c6ae19167..02401e33924 100644 --- a/types/models.d.ts +++ b/types/models.d.ts @@ -119,7 +119,7 @@ declare module 'mongoose' { AcceptsDiscriminator, IndexManager, SessionStarter { - new (doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument, TMethodsAndOverrides, 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 5753bce28d9db6b50f2c9130ef05f9314bde1543 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 24 Jun 2022 16:50:04 -0400 Subject: [PATCH 2/2] test: add test case for #11960 --- .eslintrc.json | 1 + test/types/document.test.ts | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/.eslintrc.json b/.eslintrc.json index 9112fbee2e3..2e3de0447ed 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -24,6 +24,7 @@ ], "rules": { "@typescript-eslint/triple-slash-reference": "off", + "@typescript-eslint/no-non-null-assertion": "off", "spaced-comment": [ "error", "always", diff --git a/test/types/document.test.ts b/test/types/document.test.ts index dfb476cb55f..9ee32595124 100644 --- a/test/types/document.test.ts +++ b/test/types/document.test.ts @@ -196,4 +196,45 @@ function autoTypedDocument() { // Document-Methods-tests expectType>(new AutoTypedModel().instanceFn()); +} + +async function gh11960() { + type DocumentType = Document & T; + type SubDocumentType = DocumentType & Types.Subdocument; + type ArraySubDocumentType = DocumentType & Types.ArraySubdocument; + + interface Nested { + dummy?: string; + } + + interface Parent { + username?: string; + map?: Map; + nested?: SubDocumentType; + nestedArray?: ArraySubDocumentType[]; + } + + const NestedSchema = new Schema({ + dummy: { type: String } + }); + + const ParentSchema = new Schema({ + username: { type: String }, + map: { type: Map, of: String }, + nested: { type: NestedSchema }, + nestedArray: [{ type: NestedSchema }] + }); + + const ParentModel = model>('Parent', ParentSchema); + + const doc = new ParentModel({ + username: 'user1', + map: { key1: 'value1', key2: 'value2' }, + nested: { dummy: 'hello' }, + nestedArray: [{ dummy: 'hello again' }] + }); + + expectType | undefined>(doc.map); + doc.nested!.parent(); + } \ No newline at end of file