diff --git a/test/types/connection.test.ts b/test/types/connection.test.ts index e2d86efee12..28c941c8786 100644 --- a/test/types/connection.test.ts +++ b/test/types/connection.test.ts @@ -1,4 +1,4 @@ -import { createConnection, Schema, Collection, Connection, ConnectionSyncIndexesResult, Model, connection } from 'mongoose'; +import { createConnection, Schema, Collection, Connection, ConnectionSyncIndexesResult, Model, connection, HydratedDocument, Query } from 'mongoose'; import * as mongodb from 'mongodb'; import { expectAssignable, expectError, expectType } from 'tsd'; import { AutoTypedSchemaType, autoTypedSchema } from './schema.test'; @@ -142,3 +142,42 @@ export function autoTypedModelConnection() { })(); return AutoTypedModel; } + +function schemaInstanceMethodsAndQueryHelpersOnConnection() { + 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 connection.model('User').findOne({ name }).orFail(); + } + }, + methods: { + doSomething() { + return 'test'; + } + }, + query: { + byName(this: UserModelQuery, name: string) { + return this.where({ name }); + } + } + }); + + const TestModel = connection.model('User', userSchema); +} diff --git a/types/connection.d.ts b/types/connection.d.ts index 087728a9634..8d3b7f5ff9f 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -150,7 +150,7 @@ declare module 'mongoose' { ): Model, ObtainSchemaGeneric, ObtainSchemaGeneric, {}, TSchema> & ObtainSchemaGeneric; model( name: string, - schema?: Schema, + schema?: Schema, collection?: string, options?: CompileModelOptions ): U;