diff --git a/test/types/connection.test.ts b/test/types/connection.test.ts index 3f2bc848d34..e2d86efee12 100644 --- a/test/types/connection.test.ts +++ b/test/types/connection.test.ts @@ -1,6 +1,7 @@ -import { createConnection, Schema, Collection, Connection, ConnectionSyncIndexesResult, Model } from 'mongoose'; +import { createConnection, Schema, Collection, Connection, ConnectionSyncIndexesResult, Model, connection } from 'mongoose'; import * as mongodb from 'mongodb'; -import { expectError, expectType } from 'tsd'; +import { expectAssignable, expectError, expectType } from 'tsd'; +import { AutoTypedSchemaType, autoTypedSchema } from './schema.test'; expectType(createConnection()); expectType(createConnection('mongodb://localhost:27017/test')); @@ -9,7 +10,7 @@ expectType(createConnection('mongodb://localhost:27017/test', { appName: ' const conn = createConnection(); -expectType>(conn.model('Test', new Schema<{ name: string }>({ name: { type: String } }))); +expectAssignable>(conn.model('Test', new Schema<{ name: string }>({ name: { type: String } }))); expectType>(conn.model<{ name: string }>('Test', new Schema({ name: { type: String } }))); expectType>(conn.openUri('mongodb://localhost:27017/test')); @@ -112,3 +113,32 @@ expectType(conn.useDb('test')); expectType(conn.useDb('test', {})); expectType(conn.useDb('test', { noListener: true })); expectType(conn.useDb('test', { useCache: true })); + +export function autoTypedModelConnection() { + const AutoTypedSchema = autoTypedSchema(); + const AutoTypedModel = connection.model('AutoTypeModelConnection', AutoTypedSchema); + + (async() => { + // Model-functions-test + // Create should works with arbitrary objects. + const randomObject = await AutoTypedModel.create({ unExistKey: 'unExistKey', description: 'st' }); + expectType(randomObject.userName); + + const testDoc1 = await AutoTypedModel.create({ userName: 'M0_0a' }); + expectType(testDoc1.userName); + expectType(testDoc1.description); + + const testDoc2 = await AutoTypedModel.insertMany([{ userName: 'M0_0a' }]); + expectType(testDoc2[0].userName); + expectType(testDoc2[0]?.description); + + const testDoc3 = await AutoTypedModel.findOne({ userName: 'M0_0a' }); + expectType(testDoc3?.userName); + expectType(testDoc3?.description); + + // Model-statics-functions-test + expectType>(AutoTypedModel.staticFn()); + + })(); + return AutoTypedModel; +} diff --git a/test/types/document.test.ts b/test/types/document.test.ts index 3a26d4b15b9..39f728e7b98 100644 --- a/test/types/document.test.ts +++ b/test/types/document.test.ts @@ -1,6 +1,7 @@ import { Schema, model, Model, Document, Types } from 'mongoose'; import { expectAssignable, expectError, expectType } from 'tsd'; import { autoTypedModel } from './models.test'; +import { autoTypedModelConnection } from './connection.test'; import { AutoTypedSchemaType } from './schema.test'; const Drink = model('Drink', new Schema({ @@ -198,6 +199,19 @@ function autoTypedDocument() { } +function autoTypedDocumentConnection() { + const AutoTypedModel = autoTypedModelConnection(); + const AutoTypeModelInstance = new AutoTypedModel({ unExistProperty: 1, description: 2 }); + + expectType(AutoTypeModelInstance.userName); + expectType(AutoTypeModelInstance.favoritDrink); + expectType(AutoTypeModelInstance.favoritColorMode); + + // Document-Methods-tests + expectType>(new AutoTypedModel().instanceFn()); + +} + async function gh11960() { type DocumentType = Document & T; type SubDocumentType = DocumentType & Types.Subdocument; diff --git a/types/connection.d.ts b/types/connection.d.ts index fa018e098cf..c47f7be3f81 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -142,6 +142,12 @@ declare module 'mongoose' { readonly models: Readonly<{ [index: string]: Model }>; /** Defines or retrieves a model. */ + model( + name: string, + schema?: TSchema, + collection?: string, + options?: CompileModelOptions + ): Model, ObtainSchemaGeneric, ObtainSchemaGeneric, {}, TSchema> & ObtainSchemaGeneric; model( name: string, schema?: Schema,