MongoDB Adapter options type error #1452

Closed
opened 2026-03-13 08:40:51 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @ubinatus on GitHub (Jul 1, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Declare the betterAuth config with MongoDB adatper
  2. You will see that when doing mongodbAdapter(db) (from better-auth/adapters/mongodb) it has type errors

Current vs. Expected behavior

The following has a type error:

export const auth = betterAuth({
  // ^ Types of parameters 'options' and 'options' are incompatible.
  database: mongodbAdapter(db),
});

What version of Better Auth are you using?

1.2.12

Provide environment information

- pnpm v9.15.5
- node v22.15.0
- typescript: v5.8.3
- mongodb: v6.17.0

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth";
import { MongoClient } from "mongodb";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
 
const client = new MongoClient("mongodb://localhost:27017/database");
const db = client.db();
 
export const auth = betterAuth({
  database: mongodbAdapter(db),
});

Additional context

No response

Originally created by @ubinatus on GitHub (Jul 1, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Declare the `betterAuth` config with MongoDB adatper 2. You will see that when doing `mongodbAdapter(db)` (from `better-auth/adapters/mongodb`) it has type errors ### Current vs. Expected behavior The following has a type error: ``` export const auth = betterAuth({ // ^ Types of parameters 'options' and 'options' are incompatible. database: mongodbAdapter(db), }); ``` ### What version of Better Auth are you using? 1.2.12 ### Provide environment information ```bash - pnpm v9.15.5 - node v22.15.0 - typescript: v5.8.3 - mongodb: v6.17.0 ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import { MongoClient } from "mongodb"; import { mongodbAdapter } from "better-auth/adapters/mongodb"; const client = new MongoClient("mongodb://localhost:27017/database"); const db = client.db(); export const auth = betterAuth({ database: mongodbAdapter(db), }); ``` ### Additional context _No response_
Author
Owner

@dosubot[bot] commented on GitHub (Jul 1, 2025):

Hi @ubinatus! I'm Dosu and I’m helping the better-auth team.

This type error happens because TypeScript is strict about function parameter compatibility, and the type returned by mongodbAdapter(db) doesn't always align perfectly with what betterAuth expects for its database property—even though the adapter is structurally correct and recent refactors were meant to resolve this issue.

A safe workaround is to cast the adapter to the expected type. You can do this by importing the AdapterInstance type and updating your config like this:

import { AdapterInstance } from "better-auth/types/adapter";

export const auth = betterAuth({
  database: mongodbAdapter(db) as AdapterInstance,
});

This cast is safe because mongodbAdapter is designed to match the AdapterInstance interface, and this pattern is used for other adapters as well. The underlying cause is a TypeScript assignment/contravariance issue at the call site, not a problem with the adapter itself. If you want to see more about the adapter type definitions and why this works, check the adapter types here.

Let me know if you still see issues after this change!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

@dosubot[bot] commented on GitHub (Jul 1, 2025): <!-- Greeting --> Hi @ubinatus! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This type error happens because TypeScript is strict about function parameter compatibility, and the type returned by `mongodbAdapter(db)` doesn't always align perfectly with what `betterAuth` expects for its `database` property—even though the adapter is structurally correct and recent refactors were meant to resolve this issue. A safe workaround is to cast the adapter to the expected type. You can do this by importing the `AdapterInstance` type and updating your config like this: ```ts import { AdapterInstance } from "better-auth/types/adapter"; export const auth = betterAuth({ database: mongodbAdapter(db) as AdapterInstance, }); ``` This cast is safe because `mongodbAdapter` is designed to match the `AdapterInstance` interface, and this pattern is used for other adapters as well. The underlying cause is a TypeScript assignment/contravariance issue at the call site, not a problem with the adapter itself. If you want to see more about the adapter type definitions and why this works, check the [adapter types here](https://github.com/better-auth/better-auth/blob/12e15cce41fceb36ddefcfc39fc66e082b8af699/packages/better-auth/src/types/adapter.ts). Let me know if you still see issues after this change! <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/952ac57e-d851-4e6d-8ff1-dd8ec7f9f03a?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/952ac57e-d851-4e6d-8ff1-dd8ec7f9f03a?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/952ac57e-d851-4e6d-8ff1-dd8ec7f9f03a?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/952ac57e-d851-4e6d-8ff1-dd8ec7f9f03a?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/952ac57e-d851-4e6d-8ff1-dd8ec7f9f03a?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/952ac57e-d851-4e6d-8ff1-dd8ec7f9f03a?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/952ac57e-d851-4e6d-8ff1-dd8ec7f9f03a?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3237)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1452