Type inference on additional fields not working #963

Closed
opened 2026-03-13 08:12:18 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @ian on GitHub (Apr 2, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

In the file where betterAuth config is declared:

  1. add { user: { additionalFields: { ... } } }
  2. then export type User = typeof auth.$Infer.Session.user

The inferred type of User will NOT include the additioanl fields.

Image

Current vs. Expected behavior

It's expected that type inference should be working and the User type should have the added fields from betterAuth config.

What version of Better Auth are you using?

1.2.5

Provide environment information

n/a

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

Types

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  user: {
    additionalFields: {
      bio: {
        type: 'string',
        required: false,
      },
      instagram: {
        type: 'string',
        required: false,
      },
      twitter: {
        type: 'string',
        required: false,
      },
      role: {
        type: 'string',
        required: false,
        defaultValue: 'user',
        input: false,
      },
    },
  },
});

Additional context

No response

Originally created by @ian on GitHub (Apr 2, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce In the file where betterAuth config is declared: 1. add `{ user: { additionalFields: { ... } } }` 2. then `export type User = typeof auth.$Infer.Session.user` The inferred type of User will NOT include the additioanl fields. ![Image](https://github.com/user-attachments/assets/b91298b0-768b-4afa-9914-b5f6ffc90597) ### Current vs. Expected behavior It's expected that type inference should be working and the User type should have the added fields from betterAuth config. ### What version of Better Auth are you using? 1.2.5 ### Provide environment information ```bash n/a ``` ### Which area(s) are affected? (Select all that apply) Types ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ user: { additionalFields: { bio: { type: 'string', required: false, }, instagram: { type: 'string', required: false, }, twitter: { type: 'string', required: false, }, role: { type: 'string', required: false, defaultValue: 'user', input: false, }, }, }, }); ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 08:12:18 -05:00
Author
Owner

@ian commented on GitHub (Apr 2, 2025):

I found the problem. It's because the type has to be redeclared because it's a package export, it's removing the inference.

The inferred type of 'auth' cannot be named without a reference to '../../../../node_modules/better-auth/dist/shared/better-auth.BAfIsot3'. This is likely not portable. A type annotation is necessary.

The Auth type needs to include the additional fields:

// Define the auth configuration type
const additionalFields = {
  user: {
    additionalFields: {
      bio: {
        type: 'string',
        required: false,
      },
      // other field declarations ...
    },
  },
} as const;

// Define auth type with proper return type
type Auth = ReturnType<typeof betterAuth<typeof additionalFields>>;

then you can declare the Auth type on the object:

export const auth: Auth = betterAuth({ ... })
@ian commented on GitHub (Apr 2, 2025): I found the problem. It's because the type has to be redeclared because it's a package export, it's removing the inference. > The inferred type of 'auth' cannot be named without a reference to '../../../../node_modules/better-auth/dist/shared/better-auth.BAfIsot3'. This is likely not portable. A type annotation is necessary. The Auth type needs to include the additional fields: ``` // Define the auth configuration type const additionalFields = { user: { additionalFields: { bio: { type: 'string', required: false, }, // other field declarations ... }, }, } as const; // Define auth type with proper return type type Auth = ReturnType<typeof betterAuth<typeof additionalFields>>; ``` then you can declare the Auth type on the object: ``` export const auth: Auth = betterAuth({ ... }) ```
Author
Owner

@barrynorman commented on GitHub (Apr 20, 2025):

@ian i tried it but i still cannot infer the types in the "databaseHooks"
I added "roles" as additional field, but i cannot "use" it.

Any ideas?

@barrynorman commented on GitHub (Apr 20, 2025): @ian i tried it but i still cannot infer the types in the "databaseHooks" I added "roles" as additional field, but i cannot "use" it. Any ideas?
Author
Owner

@gyenabubakar commented on GitHub (Feb 4, 2026):

Any solution? This should be reopened.

@gyenabubakar commented on GitHub (Feb 4, 2026): Any solution? This should be reopened.
Author
Owner

@gyenabubakar commented on GitHub (Feb 4, 2026):

@dosubot

@gyenabubakar commented on GitHub (Feb 4, 2026): @dosubot
Author
Owner

@dosubot[bot] commented on GitHub (Feb 4, 2026):

Here's the current state on type inference for additionalFields in better-auth:

TypeScript doesn't fully infer additionalFields in exported types or databaseHooks due to how Better Auth exposes types and how TypeScript handles package boundaries. The main workarounds are: explicitly redeclare your Auth type using ReturnType<typeof betterAuth<typeof config>> and use that type everywhere you need inference, including when exporting your auth instance and when referencing session/user types. This restores most type safety for additionalFields, but some plugin-specific typings may be lost, and hooks like databaseHooks still have limited visibility into extra fields [source].

For client-side type inference (e.g., with useSession), use the inferAdditionalFields plugin when creating your auth client and import your server Auth type into the client setup [source]. If you can't import the type, manually specify the fields in your client config.

In databaseHooks, additionalFields are not included in the user object by default. To use them, ensure your hook returns a new object that explicitly includes the fields you need [source]. This is a known limitation and there are open enhancement requests to improve support for additionalFields in hooks.

Also, check your tsconfig.json—set "exactOptionalPropertyTypes": false to avoid breaking inference for plugin and additional fields [source].

If you need strict type safety, consider using external schema tools like drizzle-zod as a fallback. Full static type inference for additionalFields is planned for future releases, so keep an eye out for updates if this is critical for your workflow.

Let me know if you have a specific scenario or error not covered here!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Feb 4, 2026): <!-- Answer --> Here's the current state on type inference for additionalFields in better-auth: TypeScript doesn't fully infer additionalFields in exported types or databaseHooks due to how Better Auth exposes types and how TypeScript handles package boundaries. The main workarounds are: explicitly redeclare your Auth type using `ReturnType<typeof betterAuth<typeof config>>` and use that type everywhere you need inference, including when exporting your auth instance and when referencing session/user types. This restores most type safety for additionalFields, but some plugin-specific typings may be lost, and hooks like databaseHooks still have limited visibility into extra fields [[source](https://github.com/better-auth/better-auth/issues/5462)]. For client-side type inference (e.g., with useSession), use the `inferAdditionalFields` plugin when creating your auth client and import your server Auth type into the client setup [[source](https://github.com/better-auth/better-auth/issues/3328)]. If you can't import the type, manually specify the fields in your client config. In databaseHooks, additionalFields are not included in the user object by default. To use them, ensure your hook returns a new object that explicitly includes the fields you need [[source](https://github.com/better-auth/better-auth/issues/7061)]. This is a known limitation and there are open enhancement requests to improve support for additionalFields in hooks. Also, check your `tsconfig.json`—set `"exactOptionalPropertyTypes": false` to avoid breaking inference for plugin and additional fields [[source](https://github.com/better-auth/better-auth/issues/5218)]. If you need strict type safety, consider using external schema tools like drizzle-zod as a fallback. Full static type inference for additionalFields is planned for future releases, so keep an eye out for updates if this is critical for your workflow. Let me know if you have a specific scenario or error not covered here! <!-- 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/4f0a1729-bccf-4916-b899-f1ffee231a85?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/4f0a1729-bccf-4916-b899-f1ffee231a85?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/4f0a1729-bccf-4916-b899-f1ffee231a85?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/4f0a1729-bccf-4916-b899-f1ffee231a85?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/4f0a1729-bccf-4916-b899-f1ffee231a85?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/4f0a1729-bccf-4916-b899-f1ffee231a85?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/4f0a1729-bccf-4916-b899-f1ffee231a85?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=Here%27s%20the%20current%20state%20on%20type%20inference%20for%20additionalFields%20in%20better-auth%3A%0A%0ATypeScript%20doesn%27t%20fully%20infer%20additionalFields%20in%20exported%20types%20or%20databaseHooks%20due%20to%20how%20Better%20Auth%20exposes%20types%20and%20how%20TypeScript%20handles%20package%20boundaries.%20The%20main%20workarounds%20are%3A%20explicitly%20redeclare%20your%20Auth%20type%20using%20%60ReturnType%3Ctypeof%20betterAuth%3Ctypeof%20config%3E%3E%60%20and%20use%20that%20type%20everywhere%20you%20need%20inference%2C%20including%20when%20exporting%20your%20auth%20instance%20and%20when%20referencing%20session/user%20types.%20This%20restores%20most%20type%20safety%20for%20additionalFields%2C%20but%20some%20plugin-specific%20typings%20may%20be%20lost%2C%20and%20hooks%20like%20databaseHooks%20still%20have%20limited%20visibility%20into%20extra%20fields%20%5B%5Bsource%5D%28https%3A//github.com/better-auth/better-auth/issues/5462%29%5D.%0A%0AFor%20client-side%20type%20inference%20%28e.g.%2C%20with%20useSession%29%2C%20use%20the%20%60inferAdditionalFields%60%20plugin%20when%20creating%20your%20auth%20client%20and%20import%20your%20server%20Auth%20type%20into%20the%20client%20setup%20%5B%5Bsource%5D%28https%3A//github.com/better-auth/better-auth/issues/3328%29%5D.%20If%20you%20can%27t%20import%20the%20type%2C%20manually%20specify%20the%20fields%20in%20your%20client%20config.%0A%0AIn%20databaseHooks%2C%20additionalFields%20are%20not%20included%20in%20the%20user%20object%20by%20default.%20To%20use%20them%2C%20ensure%20your%20hook%20returns%20a%20new%20object%20that%20explicitly%20includes%20the%20fields%20you%20need%20%5B%5Bsource%5D%28https%3A//github.com/better-auth/better-auth/issues/7061%29%5D.%20This%20is%20a%20known%20limitation%20and%20there%20are%20open%20enhancement%20requests%20to%20improve%20support%20for%20additionalFields%20in%20hooks.%0A%0AAlso%2C%20check%20your%20%60tsconfig.json%60%E2%80%94set%20%60%22exactOptionalPropertyTypes%22%3A%20false%60%20to%20avoid%20breaking%20inference%20for%20plugin%20and%20additional%20fields%20%5B%5Bsource%5D%28https%3A//github.com/better-auth/better-auth/issues/5218%29%5D.%0A%0AIf%20you%20need%20strict%20type%20safety%2C%20consider%20using%20external%20schema%20tools%20like%20drizzle-zod%20as%20a%20fallback.%20Full%20static%20type%20inference%20for%20additionalFields%20is%20planned%20for%20future%20releases%2C%20so%20keep%20an%20eye%20out%20for%20updates%20if%20this%20is%20critical%20for%20your%20workflow.%0A%0ALet%20me%20know%20if%20you%20have%20a%20specific%20scenario%20or%20error%20not%20covered%20here%21)&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/2089)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#963