From e6128cb9ffcfa3e1e3aa4b6987243d62b1595af1 Mon Sep 17 00:00:00 2001 From: Emil Janitzek Date: Wed, 27 Jul 2022 08:43:18 +0200 Subject: [PATCH 1/3] fix(types): map correct generics from model to schema --- test/types/models.test.ts | 43 ++++++++++++++++++++++++++++++++++++++- types/connection.d.ts | 2 +- types/index.d.ts | 2 +- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/test/types/models.test.ts b/test/types/models.test.ts index 62cf0e3d3bb..b97ff27d9da 100644 --- a/test/types/models.test.ts +++ b/test/types/models.test.ts @@ -7,7 +7,9 @@ import { model, Types, UpdateQuery, - CallbackError + CallbackError, + HydratedDocument, + Query } from 'mongoose'; import { expectAssignable, expectError, expectType } from 'tsd'; import { AutoTypedSchemaType, autoTypedSchema } from './schema.test'; @@ -347,6 +349,45 @@ function gh12059() { Animal.bulkSave([animal], {}); } +function schemaInstanceMethodsAndQueryHelpers() { + type UserModelQuery = Query, UserQueryHelpers> & UserQueryHelpers; + interface UserQueryHelpers { + byName(this: UserModelQuery, name: string): this + } + interface User { + name: string; + } + interface UserInstanceMethods { + doSomething(this: HydratedDocument): string; + } + interface UserStaticMethods { + findByName(name: string): Promise>; + } + type UserModel = Model & UserStaticMethods; + + const userSchema = new Schema({ + name: String + }, { + statics: { + findByName(name: string) { + return model('User').findOne({ name }).orFail(); + } + }, + methods: { + doSomething() { + return 'test'; + } + }, + query: { + byName(this: UserModelQuery, name: string) { + return this.where({ name }); + } + } + }); + + const TestModel = model('User', userSchema); +} + function gh12100() { const schema = new Schema(); diff --git a/types/connection.d.ts b/types/connection.d.ts index 62325dfbb08..01e2fca483b 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -144,7 +144,7 @@ declare module 'mongoose' { /** Defines or retrieves a model. */ model( name: string, - schema?: Schema, + schema?: Schema, collection?: string, options?: CompileModelOptions ): U; diff --git a/types/index.d.ts b/types/index.d.ts index 24c358f9a95..b03ddd8fde8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -75,7 +75,7 @@ declare module 'mongoose' { export function model( name: string, - schema?: Schema, + schema?: Schema, collection?: string, options?: CompileModelOptions ): U; From 961e591d22003be872612b2e3c9eafb21be06384 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 3 Aug 2022 15:16:53 -0400 Subject: [PATCH 2/3] Update connection.d.ts --- types/connection.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/connection.d.ts b/types/connection.d.ts index 01e2fca483b..fa018e098cf 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -144,7 +144,7 @@ declare module 'mongoose' { /** Defines or retrieves a model. */ model( name: string, - schema?: Schema, + schema?: Schema, collection?: string, options?: CompileModelOptions ): U; From 179a488caf08dc63e19e0f271d5cd0690e7cdb7e Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 3 Aug 2022 15:17:10 -0400 Subject: [PATCH 3/3] Update index.d.ts --- types/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/index.d.ts b/types/index.d.ts index b03ddd8fde8..3ffd59eecfb 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -75,7 +75,7 @@ declare module 'mongoose' { export function model( name: string, - schema?: Schema, + schema?: Schema, collection?: string, options?: CompileModelOptions ): U;