Session Management Type Inference

Hey everyone! 👋 Need some help with Better Auth type inference for custom session data
I'm having an issue with TypeScript type inference when using customSession plugin with Better Auth. Here's my setup:

Client setup:
plugins: [customSessionClient<typeof auth>(), inferAdditionalFields<typeof auth>()]

Server setup:
// Following the docs pattern with options passed to customSession
const options = { /* auth config */ } satisfies BetterAuthOptions;

export const auth = betterAuth({
  ...options,
  plugins: [
    customSession(async ({ user, session }) => {
      const userWithCompany = await prisma.user.findUnique({
        where: { id: Number(user.id) }, // Converting from old JWT auth which was setup to have the user.id as an INT, so I have the custom setting set for this to allow it
        include: { company: true },
      })
      return {
        user: { ...user, company: userWithCompany?.company || null },
        session,
      }
    }, options), // passing options as per docs
  ],
})

The Issue:

Works: When I call useSession() on the client, I get the full type with both:
Additional fields from user.additionalFields
Company data from my customSession

Doesn't work: Server-side type inference like:
export type User = typeof auth.$Infer.Session.user
// typeof auth.$Infer.Session.user.company doesn't exist here

The company property I'm adding in customSession isn't available in the inferred type on the server side (Which is where I want to define the type to use throughout the app), even though it works perfectly on the client after calling useSession().

Question: Is this expected behavior? Should custom session data only be available after calling useSession(), or am I missing something in my setup?

I'm following the docs here: https://www.better-auth.com/docs/concepts/session-management#customizing-session-response

Any insights would be super helpful! 🙏
Was this page helpful?