diff --git a/test/types/connection.test.ts b/test/types/connection.test.ts index e5f5c7ac9f2..29ddf02b3ba 100644 --- a/test/types/connection.test.ts +++ b/test/types/connection.test.ts @@ -40,7 +40,7 @@ expectType>(conn.modelNames()); expectType>(createConnection('mongodb://127.0.0.1:27017/test').close()); expectType>(createConnection('mongodb://127.0.0.1:27017/test').close(true)); -expectType(conn.db); +expectType(conn.db); expectType(conn.getClient()); expectType(conn.setClient(new mongodb.MongoClient('mongodb://127.0.0.1:27017/test'))); @@ -65,7 +65,7 @@ expectError(conn.host = 'invalid'); expectError(conn.port = 'invalid'); expectType(conn.collection('test')); -expectType(conn.db.collection('test')); +expectType(conn.db?.collection('test')); expectType>(conn.startSession()); expectType>(conn.startSession({ causalConsistency: true })); diff --git a/types/connection.d.ts b/types/connection.d.ts index b34dd226eeb..08165549a86 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -72,7 +72,7 @@ declare module 'mongoose' { readonly config: any; /** The mongodb.Db instance, set when the connection is opened */ - readonly db: mongodb.Db; + readonly db: mongodb.Db | undefined; /** * Helper for `createCollection()`. Will explicitly create the given collection