diff --git a/test/types/models.test.ts b/test/types/models.test.ts index 8d022594753..8b9f398921a 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'; @@ -329,6 +331,45 @@ function gh11911() { }); } +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 ac8594e5f0d..c1317992903 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 3cd944d0d8f..fdbceb46937 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -74,7 +74,7 @@ declare module 'mongoose' { export function model( name: string, - schema?: Schema, + schema?: Schema, collection?: string, options?: CompileModelOptions ): U;