[GH-ISSUE #8929] DatabaseHooks callbacks don't see plugin init() context extensions in types #11240

Open
opened 2026-04-13 07:35:10 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @bytaesu on GitHub (Apr 3, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8929

Is this suited for github?

  • Yes, this is suited for github

Is your feature request related to a problem? Please describe.

Issue

When a plugin returns context extensions from init(), the runtime correctly merges them using Object.assign. However, the TypeScript types for databaseHooks callbacks do not reflect these extensions.

const myPlugin = {
  id: "my-plugin",
  init() {
    return {
      context: {
        customField: null as string | null,
      },
      options: {
        databaseHooks: {
          user: {
            create: {
              before(user, ctx) {
                if (!ctx) return;
                ctx.context.customField; // TS2339: Property 'customField' does not exist
              },
            },
          },
        },
      },
    };
  },
} satisfies BetterAuthPlugin;

Cause

The ctx parameter in databaseHooks callbacks is typed as GenericEndpointContext | null, which does not include context extensions defined in init().

There is already a @ts-expect-error in with-hooks.ts:52 acknowledging this type mismatch.

While ExtractInitContext exists in plugins.ts, it is only applied to global auth options and not to databaseHooks callback signatures.

Describe the solution you'd like

Make the init() return type generic so that context extensions declared alongside databaseHooks are inferred and propagated to hook callback signatures via a shared type parameter.

Describe alternatives you've considered

No response

Additional context

No response

Originally created by @bytaesu on GitHub (Apr 3, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8929 ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. ## Issue When a plugin returns context extensions from init(), the runtime correctly merges them using Object.assign. However, the TypeScript types for databaseHooks callbacks do not reflect these extensions. ```ts const myPlugin = { id: "my-plugin", init() { return { context: { customField: null as string | null, }, options: { databaseHooks: { user: { create: { before(user, ctx) { if (!ctx) return; ctx.context.customField; // TS2339: Property 'customField' does not exist }, }, }, }, }, }; }, } satisfies BetterAuthPlugin; ``` ## Cause The ctx parameter in databaseHooks callbacks is typed as GenericEndpointContext | null, which does not include context extensions defined in init(). There is already a `@ts-expect-error` in `with-hooks.ts:52` acknowledging this type mismatch. While ExtractInitContext exists in plugins.ts, it is only applied to global auth options and not to databaseHooks callback signatures. ### Describe the solution you'd like Make the init() return type generic so that context extensions declared alongside databaseHooks are inferred and propagated to hook callback signatures via a shared type parameter. ### Describe alternatives you've considered _No response_ ### Additional context _No response_
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#11240