additionalFields not reflected in auth.$Infer.Session.user when configurations are split across files #871

Closed
opened 2026-03-13 08:08:19 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @meisolated on GitHub (Mar 18, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Steps to Reproduce:

  1. Setup better-auth Configuration:

    File: auth.ts

    import { betterAuth, type BetterAuthOptions } from "better-auth";
    import config from "../../config/config";
    import { afterHook, beforeHook } from "./authHooks";
    import advancedConfig from "./advancedConfig";
    import { pluginsConfig } from "./pluginsConfig";
    import { sessionConfig } from "./sessionConfig";
    import { userConfig } from "./userConfig";
    import { databaseConfig, secondaryStorageConfig } from "./databaseConfig";
    import { rateLimitConfig } from "./ratelimitConfig";
    import {
      accountConfig,
      emailAndPasswordConfig,
      emailVerificationConfig,
      socialProvidersConfig,
    } from "./authConfig";
    
    const betterAuthConfig = {
      trustedOrigins: [config.FRONTEND_URL],
      emailAndPassword: emailAndPasswordConfig,
      socialProviders: socialProvidersConfig,
      account: accountConfig,
      emailVerification: emailVerificationConfig,
      rateLimit: rateLimitConfig,
      hooks: {
        after: afterHook,
        before: beforeHook,
      },
      database: databaseConfig,
      secondaryStorage: secondaryStorageConfig,
      advanced: advancedConfig,
      plugins: pluginsConfig,
      session: sessionConfig,
      user: userConfig,
    } as BetterAuthOptions;
    
    export const auth = betterAuth(betterAuthConfig);
    export type User = typeof auth.$Infer.Session.user;
    export type Session = typeof auth.$Infer.Session.session;
    
  2. Define userConfig with additionalFields:

    File: userConfig.ts

    import type { BetterAuthOptions } from "better-auth";
    import { APIError } from "better-auth/api";
    
    export const userConfig: BetterAuthOptions["user"] = {
      deleteUser: {
        enabled: true,
        sendDeleteAccountVerification: async ({ user, url, token }, request) => {
          console.log(user, url, token, request?.formData);
        },
        beforeDelete: async (user) => {
          if (user.email.includes("admin")) {
            throw new APIError("BAD_REQUEST", {
              message: "Admin accounts can't be deleted",
            });
          }
        },
        afterDelete: async (user) => {
          console.log(user);
        },
      },
      additionalFields: {
        jobTitle: {
          type: "string",
          required: false,
          defaultValue: "",
        },
      },
    } satisfies BetterAuthOptions["user"];
    
  3. Try to Access jobTitle in Inferred User Type:

    In your application, try to access the jobTitle property from the User type:

    import { User } from "./auth";
    
    const user: User = /* ... */;
    console.log(user.jobTitle); // Property 'jobTitle' does not exist on type 'User'
    

Current vs. Expected behavior

Expected Behavior:

The User type (auth.$Infer.Session.user) should include the jobTitle property defined in additionalFields, allowing for type-safe access:

user.jobTitle; // Should be of type 'string'

Actual Behavior:

The User type does not include the jobTitle property. Attempting to access it results in a TypeScript error:

Property 'jobTitle' does not exist on type 'User'

What version of Better Auth are you using?

1.2.4

Provide environment information

- OS : Ubuntu 
- Node : v20.14.0
- Typescript Version : 5.7.3 using bun

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

Backend

Auth config (if applicable)


Additional context

Additional Information:

  • When the entire configuration, including additionalFields, is placed within the same file (auth.ts), the jobTitle field is correctly inferred in the User type.
  • It seems that splitting the configuration across multiple files causes the type inference for additionalFields to fail.
Originally created by @meisolated on GitHub (Mar 18, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce **Steps to Reproduce:** 1. **Setup `better-auth` Configuration:** **File: `auth.ts`** ```typescript import { betterAuth, type BetterAuthOptions } from "better-auth"; import config from "../../config/config"; import { afterHook, beforeHook } from "./authHooks"; import advancedConfig from "./advancedConfig"; import { pluginsConfig } from "./pluginsConfig"; import { sessionConfig } from "./sessionConfig"; import { userConfig } from "./userConfig"; import { databaseConfig, secondaryStorageConfig } from "./databaseConfig"; import { rateLimitConfig } from "./ratelimitConfig"; import { accountConfig, emailAndPasswordConfig, emailVerificationConfig, socialProvidersConfig, } from "./authConfig"; const betterAuthConfig = { trustedOrigins: [config.FRONTEND_URL], emailAndPassword: emailAndPasswordConfig, socialProviders: socialProvidersConfig, account: accountConfig, emailVerification: emailVerificationConfig, rateLimit: rateLimitConfig, hooks: { after: afterHook, before: beforeHook, }, database: databaseConfig, secondaryStorage: secondaryStorageConfig, advanced: advancedConfig, plugins: pluginsConfig, session: sessionConfig, user: userConfig, } as BetterAuthOptions; export const auth = betterAuth(betterAuthConfig); export type User = typeof auth.$Infer.Session.user; export type Session = typeof auth.$Infer.Session.session; ``` 2. **Define `userConfig` with `additionalFields`:** **File: `userConfig.ts`** ```typescript import type { BetterAuthOptions } from "better-auth"; import { APIError } from "better-auth/api"; export const userConfig: BetterAuthOptions["user"] = { deleteUser: { enabled: true, sendDeleteAccountVerification: async ({ user, url, token }, request) => { console.log(user, url, token, request?.formData); }, beforeDelete: async (user) => { if (user.email.includes("admin")) { throw new APIError("BAD_REQUEST", { message: "Admin accounts can't be deleted", }); } }, afterDelete: async (user) => { console.log(user); }, }, additionalFields: { jobTitle: { type: "string", required: false, defaultValue: "", }, }, } satisfies BetterAuthOptions["user"]; ``` 3. **Try to Access `jobTitle` in Inferred `User` Type:** In your application, try to access the `jobTitle` property from the `User` type: ```typescript import { User } from "./auth"; const user: User = /* ... */; console.log(user.jobTitle); // Property 'jobTitle' does not exist on type 'User' ``` ### Current vs. Expected behavior **Expected Behavior:** The `User` type (`auth.$Infer.Session.user`) should include the `jobTitle` property defined in `additionalFields`, allowing for type-safe access: ```typescript user.jobTitle; // Should be of type 'string' ``` **Actual Behavior:** The `User` type does not include the `jobTitle` property. Attempting to access it results in a TypeScript error: ``` Property 'jobTitle' does not exist on type 'User' ``` ### What version of Better Auth are you using? 1.2.4 ### Provide environment information ```bash - OS : Ubuntu - Node : v20.14.0 - Typescript Version : 5.7.3 using bun ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript ``` ### Additional context **Additional Information:** - When the entire configuration, including `additionalFields`, is placed within the same file (`auth.ts`), the `jobTitle` field is correctly inferred in the `User` type. - It seems that splitting the configuration across multiple files causes the type inference for `additionalFields` to fail.
GiteaMirror added the bug label 2026-03-13 08:08:19 -05:00
Author
Owner

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

How was this solved? I'm still seeing the issue.

@ian commented on GitHub (Apr 2, 2025): How was this solved? I'm still seeing the issue.
Author
Owner

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

How was this solved? I'm still seeing the issue.

I messed up actually, I was just suppose to remove.

For example

satisfies BetterAuthOptions["user"];

and use in auth.ts

 satisfies BetterAuthOptions
@meisolated commented on GitHub (Apr 2, 2025): > How was this solved? I'm still seeing the issue. I messed up actually, I was just suppose to remove. For example ```ts satisfies BetterAuthOptions["user"]; ``` and use in auth.ts ```ts satisfies BetterAuthOptions ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#871