[GH-ISSUE #219] additionalFields are not inferred if BetterAuthOptions are extracted #8176

Closed
opened 2026-04-13 03:16:36 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @Loa212 on GitHub (Oct 17, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/219

It is not possible to extract BetterAuthOptions and have additionalFields inferred in the user type.

working:


export const auth = betterAuth({
  user: {
    additionalFields: {
      example: {
        type: "string",
      },
    },
  },
  [... more config]
});

export const getServerAuthSession = () =>
  auth.api.getSession({
    headers: headers(),
  });

type GetServerAuthSessionResult = NonNullable<
  Awaited<ReturnType<typeof getServerAuthSession>>
>;

type _test = Expect<ToHaveKey<GetServerAuthSessionResult["user"], "example">>; // true

In the below example instead, additionalFields are not inferred.


const user:BetterAuthOptions["user"] = {
  additionalFields: {
    example: {
      type: "string",
    },
  },
}

export const auth = betterAuth({
  user: user,
  [... more config]
});

export const getServerAuthSession = () =>
  auth.api.getSession({
    headers: headers(),
  });

type GetServerAuthSessionResult = NonNullable<
  Awaited<ReturnType<typeof getServerAuthSession>>
>;

type _test = Expect<ToHaveKey<GetServerAuthSessionResult["user"], "example">>;  // false

I am using MacOs

project is Nextjs + TRPC + drizzleORM

Also I am running on 0.4.12

Originally created by @Loa212 on GitHub (Oct 17, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/219 It is not possible to extract `BetterAuthOptions` and have `additionalFields` inferred in the `user` type. working: ```ts export const auth = betterAuth({ user: { additionalFields: { example: { type: "string", }, }, }, [... more config] }); export const getServerAuthSession = () => auth.api.getSession({ headers: headers(), }); type GetServerAuthSessionResult = NonNullable< Awaited<ReturnType<typeof getServerAuthSession>> >; type _test = Expect<ToHaveKey<GetServerAuthSessionResult["user"], "example">>; // true ``` In the below example instead, additionalFields are not inferred. ```ts const user:BetterAuthOptions["user"] = { additionalFields: { example: { type: "string", }, }, } export const auth = betterAuth({ user: user, [... more config] }); export const getServerAuthSession = () => auth.api.getSession({ headers: headers(), }); type GetServerAuthSessionResult = NonNullable< Awaited<ReturnType<typeof getServerAuthSession>> >; type _test = Expect<ToHaveKey<GetServerAuthSessionResult["user"], "example">>; // false ``` I am using MacOs project is Nextjs + TRPC + drizzleORM Also I am running on `0.4.12`
GiteaMirror added the locked label 2026-04-13 03:16:36 -05:00
Author
Owner

@Bekacru commented on GitHub (Oct 18, 2024):

typescript will not infer types like that since the default will be casted. If you need to extract the options make sure, to use satisfies instead.

const options = {
   //your config
} satisfies BetterAuthOptions
<!-- gh-comment-id:2421521119 --> @Bekacru commented on GitHub (Oct 18, 2024): typescript will not infer types like that since the default will be casted. If you need to extract the options make sure, to use `satisfies` instead. ```ts const options = { //your config } 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#8176