additionalFields are not inferred if BetterAuthOptions are extracted #92

Closed
opened 2026-03-13 07:32:53 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @Loa212 on GitHub (Oct 17, 2024).

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). 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`
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
@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#92