[PR #2158] [CLOSED] refactor: betterAuth fn now infers models #12430

Closed
opened 2026-04-13 08:22:35 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/2158
Author: @ping-maxwell
Created: 4/6/2025
Status: Closed

Base: mainHead: fix/better-auth-options/model-inference


📝 Commits (6)

  • b04ae5f refactor: betterAuth fn now infers models
  • c9e86c4 fix: type errors
  • 8b8d1bb refactor: BetterAuthOptions type definition
  • 9d7e0fa Merge branch 'main' into fix/better-auth-options/model-inference
  • 7dc557c Merge branch 'main' into fix/better-auth-options/model-inference
  • d2a1ba9 fix: type errors

📊 Changes

6 files changed (+378 additions, -74 deletions)

View changed files

📝 packages/better-auth/src/auth.ts (+77 -12)
📝 packages/better-auth/src/cookies/index.ts (+1 -0)
📝 packages/better-auth/src/db/field.ts (+8 -5)
📝 packages/better-auth/src/types/helper.ts (+4 -0)
📝 packages/better-auth/src/types/models.ts (+22 -8)
📝 packages/better-auth/src/types/options.ts (+266 -49)

📄 Description

We now infer models based on the options:

image

Previously:

image

How?

The betterAuth function now has generics for plugins, as well as any model data that could be modified by options, such as User additional fields.

Changes are straightforward:

  • All BetterAuthOptions are moved straight into the betterAuth function parameters.
  • The betterAuth function has generics that track any model inference related options.
  • The old BetterAuthOptions type export is replaced with just a Parameters<typeof betterAuth>[0] sort of thing.
    • That BetterAuthOptions also includes all generics similar to betterAuth, but has default values so don't worry about having each possible BetterAuthOptions needing to provide generic values, nothing should break. I made it possible to pass the generics similar to betterAuth for the purpose of type inferencing within the betterAuth function code.

The result is a nice experience working with the betterAuth function knowing full-well that any methods within the options will be type-safe regarding model objects.

However this does come with a cost:
Any function which in-takes a BetterAuthOptions will not be equal to the options we'll end up getting from the betterAuth function parameters.
This is because our options from the BA function has generics, thus meaning some values could be dynamic/different.
In reality, they will 100% be the same/equal, if comparing BetterAuthOptions to the result options object.
The reason I say this is a cost is because anytime we have to call a function within the betterAuth function, and it in-takes options that's of type BetterAuthOptions, it will throw a type error saying it doesn't match.
To reemphasize, this is just because there is a difference in generics.

My solution to this is just to use a ts-ignore, since they're technically still the same BetterAuthOptions.
The alternative solution is to have each of those function to also in-take every possible generic to build the same BetterAuthOptions to equal options.
But this would mean we need to do these generics for each function we end up using. Not to mention maintaining this.

Using ts-ignore was just easier. It's just a type-err within the betterAuth function, so no end-users would deal with this.

In cases where inference from the BA options is required we would then fully type it, and not use ts-ignore.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/2158 **Author:** [@ping-maxwell](https://github.com/ping-maxwell) **Created:** 4/6/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/better-auth-options/model-inference` --- ### 📝 Commits (6) - [`b04ae5f`](https://github.com/better-auth/better-auth/commit/b04ae5ffb953a4e890c03ba44e3b0145d4414933) refactor: `betterAuth` fn now infers models - [`c9e86c4`](https://github.com/better-auth/better-auth/commit/c9e86c4a59589abe709fa6cf83dccb50626c42ed) fix: type errors - [`8b8d1bb`](https://github.com/better-auth/better-auth/commit/8b8d1bb299af747abac6965abfaa6b75a846e87c) refactor: `BetterAuthOptions` type definition - [`9d7e0fa`](https://github.com/better-auth/better-auth/commit/9d7e0fa6d241436a7c1cf397b0c5417a3991c9f8) Merge branch 'main' into fix/better-auth-options/model-inference - [`7dc557c`](https://github.com/better-auth/better-auth/commit/7dc557cdf756050bc5f9493f86a55dc0304731ed) Merge branch 'main' into fix/better-auth-options/model-inference - [`d2a1ba9`](https://github.com/better-auth/better-auth/commit/d2a1ba9550c86a9ac983cfc34bb30af9e552a1df) fix: type errors ### 📊 Changes **6 files changed** (+378 additions, -74 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/auth.ts` (+77 -12) 📝 `packages/better-auth/src/cookies/index.ts` (+1 -0) 📝 `packages/better-auth/src/db/field.ts` (+8 -5) 📝 `packages/better-auth/src/types/helper.ts` (+4 -0) 📝 `packages/better-auth/src/types/models.ts` (+22 -8) 📝 `packages/better-auth/src/types/options.ts` (+266 -49) </details> ### 📄 Description We now infer models based on the options: <img width="908" alt="image" src="https://github.com/user-attachments/assets/cec57e8a-954e-4073-98b6-74d65bc49c9c" /> Previously: <img width="682" alt="image" src="https://github.com/user-attachments/assets/7aafc80b-cae0-4f0b-82c2-d4a3291b97a8" /> --- ## How? The `betterAuth` function now has generics for plugins, as well as any model data that could be modified by `options`, such as User additional fields. Changes are straightforward: * All `BetterAuthOptions` are moved straight into the `betterAuth` function parameters. * The `betterAuth` function has generics that track any model inference related options. * The old `BetterAuthOptions` type export is replaced with just a `Parameters<typeof betterAuth>[0]` sort of thing. * That `BetterAuthOptions` **also** includes all generics similar to `betterAuth`, but has default values so don't worry about having each possible `BetterAuthOptions` needing to provide generic values, nothing should break. I made it possible to pass the generics similar to `betterAuth` for the purpose of type inferencing within the `betterAuth` function code. The result is a nice experience working with the `betterAuth` function knowing full-well that any methods within the options will be type-safe regarding model objects. However this does come with a cost: Any function which in-takes a `BetterAuthOptions` **will not** be equal to the `options` we'll end up getting from the `betterAuth` function parameters. This is because our `options` from the BA function has generics, thus meaning some values could be dynamic/different. In reality, they will 100% be the same/equal, if comparing BetterAuthOptions to the result `options` object. The reason I say this is a cost is because anytime we have to call a function within the `betterAuth` function, and it in-takes `options` that's of type `BetterAuthOptions`, it will throw a type error saying it doesn't match. To reemphasize, this is just because there is a difference in generics. My solution to this is just to use a ts-ignore, since they're technically still the same BetterAuthOptions. The alternative solution is to have each of those function to also in-take every possible generic to build the same `BetterAuthOptions` to equal `options`. But this would mean we need to do these generics for each function we end up using. Not to mention maintaining this. Using `ts-ignore` was just easier. It's just a type-err within the `betterAuth` function, so no end-users would deal with this. In cases where inference from the BA options is required we would then fully type it, and not use ts-ignore. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 08:22:35 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#12430