From ae0661946a84ff5f379e4271c2f6613f24e97a11 Mon Sep 17 00:00:00 2001
From: Poorshad
Date: Tue, 5 Sep 2023 00:37:06 +0200
Subject: [PATCH 1/3] types(schematypes): use DocType for method this
---
test/types/schema.test.ts | 31 +++++++++++++++++++++++++++++++
types/index.d.ts | 2 +-
2 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts
index 45dd2653c31..eb38016bef1 100644
--- a/test/types/schema.test.ts
+++ b/test/types/schema.test.ts
@@ -1183,3 +1183,34 @@ function gh13780() {
type InferredType = InferSchemaType;
expectType(null as unknown as InferredType['num']);
}
+
+function gh13800() {
+ interface IUser {
+ firstName: string;
+ lastName: string;
+ someOtherField: string;
+ }
+ interface IUserMethods {
+ fullName(): string;
+ }
+ type UserModel = Model;
+
+ // Typed Schema
+ const schema = new Schema({
+ firstName: { type: String, required: true },
+ lastName: { type: String, required: true }
+ });
+ schema.method('fullName', function fullName() {
+ type This = typeof this;
+ expectType(this);
+ });
+
+ // Auto Typed Schema
+ const autoTypedSchema = new Schema({
+ firstName: { type: String, required: true },
+ lastName: { type: String, required: true }
+ });
+ autoTypedSchema.method('fullName', function fullName() {
+ expectType>(this);
+ });
+}
\ No newline at end of file
diff --git a/types/index.d.ts b/types/index.d.ts
index 95653baf5a4..fe36ffa2217 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -293,7 +293,7 @@ declare module 'mongoose' {
loadClass(model: Function, onlyVirtuals?: boolean): this;
/** Adds an instance method to documents constructed from Models compiled from this schema. */
- method(name: string, fn: (this: Context, ...args: any[]) => any, opts?: any): this;
+ method(name: string, fn: (this: Context, ...args: any[]) => any, opts?: any): this;
method(obj: Partial): this;
/** Object of currently defined methods on this schema. */
From 62a61d696ad0fe66d69db8af57b84e0d45277300 Mon Sep 17 00:00:00 2001
From: Poorshad
Date: Tue, 5 Sep 2023 16:39:35 +0200
Subject: [PATCH 2/3] use THydratedDocumentType type for this in instance
methods
---
test/types/schema.test.ts | 3 +--
types/index.d.ts | 2 +-
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts
index eb38016bef1..a4e40c9e490 100644
--- a/test/types/schema.test.ts
+++ b/test/types/schema.test.ts
@@ -1201,8 +1201,7 @@ function gh13800() {
lastName: { type: String, required: true }
});
schema.method('fullName', function fullName() {
- type This = typeof this;
- expectType(this);
+ expectType(this);
});
// Auto Typed Schema
diff --git a/types/index.d.ts b/types/index.d.ts
index fe36ffa2217..840a3634e16 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -293,7 +293,7 @@ declare module 'mongoose' {
loadClass(model: Function, onlyVirtuals?: boolean): this;
/** Adds an instance method to documents constructed from Models compiled from this schema. */
- method(name: string, fn: (this: Context, ...args: any[]) => any, opts?: any): this;
+ method(name: string, fn: (this: Context, ...args: any[]) => any, opts?: any): this;
method(obj: Partial): this;
/** Object of currently defined methods on this schema. */
From 8debef7c73c5c6f452f1c5e36d22e5a6afb0f41a Mon Sep 17 00:00:00 2001
From: Poorshad
Date: Tue, 5 Sep 2023 19:33:46 +0200
Subject: [PATCH 3/3] check types of fields individually in the test
---
test/types/schema.test.ts | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts
index a4e40c9e490..b6726bfdc78 100644
--- a/test/types/schema.test.ts
+++ b/test/types/schema.test.ts
@@ -1201,7 +1201,10 @@ function gh13800() {
lastName: { type: String, required: true }
});
schema.method('fullName', function fullName() {
- expectType(this);
+ expectType(this.firstName);
+ expectType(this.lastName);
+ expectType(this.someOtherField);
+ expectType(this.fullName);
});
// Auto Typed Schema
@@ -1210,6 +1213,8 @@ function gh13800() {
lastName: { type: String, required: true }
});
autoTypedSchema.method('fullName', function fullName() {
- expectType>(this);
+ expectType(this.firstName);
+ expectType(this.lastName);
+ expectError(this.someOtherField);
});
}
\ No newline at end of file