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); +}