From a1c09447cfe25caa6c39f3c80464dd745ac75e70 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 21 Feb 2023 11:00:30 -0500 Subject: [PATCH 1/3] fix(types): use MergeTypes for type overrides in HydratedDocument Fix #13040 --- package.json | 4 ++-- test/types/lean.test.ts | 2 +- types/index.d.ts | 8 +++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 47930c2e8dc..332937da548 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,8 @@ "@babel/preset-env": "7.20.2", "@typescript-eslint/eslint-plugin": "5.50.0", "@typescript-eslint/parser": "5.50.0", - "acquit": "1.2.1", - "acquit-ignore": "0.2.0", + "acquit": "1.3.0", + "acquit-ignore": "0.2.1", "acquit-require": "0.1.1", "assert-browserify": "2.0.0", "axios": "1.1.3", diff --git a/test/types/lean.test.ts b/test/types/lean.test.ts index 6b4bfccb201..2a7088812a3 100644 --- a/test/types/lean.test.ts +++ b/test/types/lean.test.ts @@ -182,7 +182,7 @@ async function getBaseDocumentTypeFromModel(): Promise { type baseFromUserDocType = BaseDocumentType; - expectType({} as baseFromUserDocType); + expectType>({} as baseFromUserDocType); const a: UserDocType = {} as any; diff --git a/types/index.d.ts b/types/index.d.ts index 640355a15b8..ac24805858c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -129,12 +129,14 @@ declare module 'mongoose' { ? IfAny> : T & { _id: Types.ObjectId }; - export type HydratedDocument = DocType extends Document ? Require_id : (Document & Require_id & TVirtuals & TMethodsAndOverrides); + export type HydratedDocument = DocType extends Document ? + Require_id : + Document & MergeType, TVirtuals & TMethodsAndOverrides>; export type HydratedDocumentFromSchema = HydratedDocument< InferSchemaType, - ObtainSchemaGeneric, - ObtainSchemaGeneric + ObtainSchemaGeneric, + ObtainSchemaGeneric >; export interface TagSet { From d7f7b4234cf9b7c392c119f7f5fe0e6ec495f54b Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 21 Feb 2023 11:16:54 -0500 Subject: [PATCH 2/3] test: add test case for #13040 --- test/types/subdocuments.test.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/types/subdocuments.test.ts b/test/types/subdocuments.test.ts index 58fe37162a6..300fe7d6eb7 100644 --- a/test/types/subdocuments.test.ts +++ b/test/types/subdocuments.test.ts @@ -76,3 +76,35 @@ function gh10674() { async function gh10947(): Promise { await Test.findOneAndUpdate({}, { child1: { name: 'foo' } }); } + +function gh13040(): void { + interface Product {} + + interface User { + products: Product[]; + } + + // I want my `UserDocument` type to define `products` as a `DocumentArray`; I'll do this using overrides + + interface UserOverrides { + products: Types.DocumentArray; + } + + interface UserModel extends Model {} + + // Here I determine the type of user documents; I could also manually define a `HydratedDocument` - makes no difference. + + interface UserDocument extends InstanceType {} + + // Assume I have an instance of `UserDocument` + + let user!: UserDocument; + + // This is then fine: + + user.products[0].ownerDocument(); // ok + + user.products.forEach(product => { + product.ownerDocument(); + }); +} \ No newline at end of file From 170d8fc14c4ad7401e09cfb7fcba0683592d840b Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 21 Feb 2023 13:48:00 -0500 Subject: [PATCH 3/3] style: fix lint --- test/types/.eslintrc.yml | 2 ++ test/types/subdocuments.test.ts | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 test/types/.eslintrc.yml diff --git a/test/types/.eslintrc.yml b/test/types/.eslintrc.yml new file mode 100644 index 00000000000..7e081732529 --- /dev/null +++ b/test/types/.eslintrc.yml @@ -0,0 +1,2 @@ +rules: + "@typescript-eslint/no-empty-interface": off \ No newline at end of file diff --git a/test/types/subdocuments.test.ts b/test/types/subdocuments.test.ts index 300fe7d6eb7..5b690a44fcb 100644 --- a/test/types/subdocuments.test.ts +++ b/test/types/subdocuments.test.ts @@ -90,11 +90,11 @@ function gh13040(): void { products: Types.DocumentArray; } - interface UserModel extends Model {} + type UserModel = Model; // Here I determine the type of user documents; I could also manually define a `HydratedDocument` - makes no difference. - interface UserDocument extends InstanceType {} + type UserDocument = InstanceType; // Assume I have an instance of `UserDocument` @@ -107,4 +107,4 @@ function gh13040(): void { user.products.forEach(product => { product.ownerDocument(); }); -} \ No newline at end of file +}