[GH-ISSUE #3627] [Bug] [Bug] TS4023 error occurs after using the admin plugin #26991

Closed
opened 2026-04-17 17:45:55 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @daidr on GitHub (Jul 26, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/3627

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

auth.ts

import { betterAuth } from 'better-auth'
import { admin } from 'better-auth/plugins'

export const auth = betterAuth({
  plugins: [
    admin(),
  ],
})

tsconfig.json

{
  // ...
  "declaration": true,
  "declarationMap": true,
}

Current vs. Expected behavior

The error occurred:

./utils/auth.ts(8,14): error TS4023: Exported variable 'auth' has or is using name 'SessionWithImpersonatedBy' from external module "xxx/node_modules/.bun/better-auth@1.3.3/node_modules/better-auth/dist/plugins/admin/index" but cannot be named.
./core/auth.ts(8,14): error TS4023: Exported variable 'auth' has or is using name 'UserWithRole' from external module "xxx/node_modules/.bun/better-auth@1.3.3/node_modules/better-auth/dist/plugins/admin/index" but cannot be named.

I expect there to be no error output.

What version of Better Auth are you using?

1.3.3

Provide environment information

OS: Windows 11
Runtime: Bun v1.2.19

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

Types

Auth config (if applicable)

import { betterAuth } from 'better-auth'
import { admin } from 'better-auth/plugins'

export const auth = betterAuth({
  plugins: [
    admin(),
  ],
})

Additional context

I'm not sure if it's related to my use of a monorepo (workspace).

Originally created by @daidr on GitHub (Jul 26, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/3627 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce auth.ts ```ts import { betterAuth } from 'better-auth' import { admin } from 'better-auth/plugins' export const auth = betterAuth({ plugins: [ admin(), ], }) ``` tsconfig.json ```json5 { // ... "declaration": true, "declarationMap": true, } ``` ### Current vs. Expected behavior The error occurred: ``` ./utils/auth.ts(8,14): error TS4023: Exported variable 'auth' has or is using name 'SessionWithImpersonatedBy' from external module "xxx/node_modules/.bun/better-auth@1.3.3/node_modules/better-auth/dist/plugins/admin/index" but cannot be named. ./core/auth.ts(8,14): error TS4023: Exported variable 'auth' has or is using name 'UserWithRole' from external module "xxx/node_modules/.bun/better-auth@1.3.3/node_modules/better-auth/dist/plugins/admin/index" but cannot be named. ``` I expect there to be no error output. ### What version of Better Auth are you using? 1.3.3 ### Provide environment information ```bash OS: Windows 11 Runtime: Bun v1.2.19 ``` ### Which area(s) are affected? (Select all that apply) Types ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth' import { admin } from 'better-auth/plugins' export const auth = betterAuth({ plugins: [ admin(), ], }) ``` ### Additional context I'm not sure if it's related to my use of a monorepo (workspace).
GiteaMirror added the lockedbug labels 2026-04-17 17:45:55 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 26, 2025):

Hi @daidr! I'm Dosu and I’m helping the better-auth team.

This TS4023 error happens because the admin plugin uses types like SessionWithImpersonatedBy and UserWithRole in its public API, but these types are not re-exported from the plugin’s main entry point. When TypeScript tries to generate declaration files with declaration: true, it can’t name these types, so it throws TS4023. This is a known TypeScript issue in plugin architectures and is not specific to your monorepo setup—though monorepos can sometimes make type resolution trickier, the root cause here is missing type re-exports in the admin plugin itself [source].

Workarounds:

  • Set declaration: false in your tsconfig to avoid .d.ts generation (quickest, but you lose declaration files) [source].
  • Use type assertions like admin() as unknown as BetterAuthPlugin (works, but you lose type inference) [source].
  • Explicitly annotate types in your code instead of relying on inference.

A proper fix would be for the admin plugin to re-export these types from its main entry point (e.g., admin.ts or index.ts). Recent PRs have fixed similar issues in other plugins, but as of v1.3.3, the admin plugin still has this gap [source].

Until an official fix lands, the best options are to disable declaration output or use type assertions. If you need declaration files, you could also fork and patch the plugin to re-export the missing types.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

<!-- gh-comment-id:3121540409 --> @dosubot[bot] commented on GitHub (Jul 26, 2025): <!-- Greeting --> Hi @daidr! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This TS4023 error happens because the admin plugin uses types like `SessionWithImpersonatedBy` and `UserWithRole` in its public API, but these types are not re-exported from the plugin’s main entry point. When TypeScript tries to generate declaration files with `declaration: true`, it can’t name these types, so it throws TS4023. This is a known TypeScript issue in plugin architectures and is not specific to your monorepo setup—though monorepos can sometimes make type resolution trickier, the root cause here is missing type re-exports in the admin plugin itself [[source]](https://github.com/better-auth/better-auth/blob/02f19810d60a26a8907f6a988383edf91b0b9599/packages/better-auth/src/plugins/admin/types.ts). Workarounds: - Set `declaration: false` in your tsconfig to avoid .d.ts generation (quickest, but you lose declaration files) [[source]](https://github.com/better-auth/better-auth/issues/2789). - Use type assertions like `admin() as unknown as BetterAuthPlugin` (works, but you lose type inference) [[source]](https://github.com/better-auth/better-auth/issues/2789). - Explicitly annotate types in your code instead of relying on inference. A proper fix would be for the admin plugin to re-export these types from its main entry point (e.g., `admin.ts` or `index.ts`). Recent PRs have fixed similar issues in other plugins, but as of v1.3.3, the admin plugin still has this gap [[source]](https://github.com/better-auth/better-auth/pull/1270). Until an official fix lands, the best options are to disable declaration output or use type assertions. If you need declaration files, you could also fork and patch the plugin to re-export the missing types. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/0ce3159c-4f14-4079-ace6-14cdcf3793be?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/0ce3159c-4f14-4079-ace6-14cdcf3793be?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/0ce3159c-4f14-4079-ace6-14cdcf3793be?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/0ce3159c-4f14-4079-ace6-14cdcf3793be?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/0ce3159c-4f14-4079-ace6-14cdcf3793be?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/0ce3159c-4f14-4079-ace6-14cdcf3793be?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/0ce3159c-4f14-4079-ace6-14cdcf3793be?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3627)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#26991