Type inference of createAuthMiddleware, optionsMiddleware, createAuthEndpoint broken as of 1.3.24 #2168

Closed
opened 2026-03-13 09:31:44 -05:00 by GiteaMirror · 7 comments
Owner

Originally created by @astrodomas on GitHub (Oct 21, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

import type { createAuthMiddleware } from "better-auth/plugins";

type TCreateAuthMiddlewareCallback = Parameters<typeof createAuthMiddleware>[0];

// typeof TCreateAuthMiddlewareCallback will be `any`

It appears that all of the following functions are affected when exported under:

// better-auth/dist/plugins/index.d.ts
...
export { AuthEndpoint, AuthMiddleware, createAuthEndpoint, createAuthMiddleware, optionsMiddleware } from '@better-auth/core/middleware';
...
Image Image

Current vs. Expected behavior

The last working version was 1.3.19.

What version of Better Auth are you using?

1.3.28

System info

"system": {
    "platform": "win32",
    "arch": "x64",
    "version": "Windows 11 Pro",
    "release": "10.0.22621",
    "cpuCount": 16,
    "cpuModel": "12th Gen Intel(R) Core(TM) i7-1260P",
    "totalMemory": "31.70 GB",
    "freeMemory": "15.91 GB"
  },
  "node": {
    "version": "v22.20.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "10.9.3"
  },
  "frameworks": [
    {
      "name": "express",
      "version": "^5.1.0"
    }
  ],
  "databases": [
    {
      "name": "@prisma/client",
      "version": "^6.16.2"
    }
  ],

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

Types

Auth config (if applicable)

unrelated

Additional context

// tsconfig.json
{
  "compilerOptions": {
    "incremental": true,
    "target": "es6",
    "module": "commonjs",
    "rootDir": "./",
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {
      "@/*": ["./src/*"],
      "@templates/*": ["src/templates/*"]
    },
    "typeRoots": ["./src/@types", "./node_modules/@types"],
    "types": ["node", "jest"],
    "resolveJsonModule": true,
    "allowJs": true,
    "declaration": true,
    "sourceMap": true,
    "outDir": "./dist",
    "removeComments": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictBindCallApply": true,
    "noFallthroughCasesInSwitch": true ,
    "skipLibCheck": true
  },
  "include": ["src/**/*", "scripts"],
  "exclude": ["node_modules"]
}

Originally created by @astrodomas on GitHub (Oct 21, 2025). ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce ``` import type { createAuthMiddleware } from "better-auth/plugins"; type TCreateAuthMiddlewareCallback = Parameters<typeof createAuthMiddleware>[0]; // typeof TCreateAuthMiddlewareCallback will be `any` ``` It appears that all of the following functions are affected when exported under: ``` // better-auth/dist/plugins/index.d.ts ... export { AuthEndpoint, AuthMiddleware, createAuthEndpoint, createAuthMiddleware, optionsMiddleware } from '@better-auth/core/middleware'; ... ``` <img width="1238" height="617" alt="Image" src="https://github.com/user-attachments/assets/fc4d13a3-d7a9-4e3c-a0cc-905faa2f75cf" /> <img width="1624" height="536" alt="Image" src="https://github.com/user-attachments/assets/a3532da4-1668-4cdd-be66-a474dd7d1c14" /> ### Current vs. Expected behavior The last working version was 1.3.19. ### What version of Better Auth are you using? 1.3.28 ### System info ```bash "system": { "platform": "win32", "arch": "x64", "version": "Windows 11 Pro", "release": "10.0.22621", "cpuCount": 16, "cpuModel": "12th Gen Intel(R) Core(TM) i7-1260P", "totalMemory": "31.70 GB", "freeMemory": "15.91 GB" }, "node": { "version": "v22.20.0", "env": "development" }, "packageManager": { "name": "npm", "version": "10.9.3" }, "frameworks": [ { "name": "express", "version": "^5.1.0" } ], "databases": [ { "name": "@prisma/client", "version": "^6.16.2" } ], ``` ### Which area(s) are affected? (Select all that apply) Types ### Auth config (if applicable) ```typescript unrelated ``` ### Additional context ```json // tsconfig.json { "compilerOptions": { "incremental": true, "target": "es6", "module": "commonjs", "rootDir": "./", "moduleResolution": "node", "baseUrl": "./", "paths": { "@/*": ["./src/*"], "@templates/*": ["src/templates/*"] }, "typeRoots": ["./src/@types", "./node_modules/@types"], "types": ["node", "jest"], "resolveJsonModule": true, "allowJs": true, "declaration": true, "sourceMap": true, "outDir": "./dist", "removeComments": true, "allowSyntheticDefaultImports": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "noImplicitAny": true, "strictNullChecks": true, "strictBindCallApply": true, "noFallthroughCasesInSwitch": true , "skipLibCheck": true }, "include": ["src/**/*", "scripts"], "exclude": ["node_modules"] } ```
Author
Owner

@Bekacru commented on GitHub (Oct 21, 2025):

I couldn't reproduce this from my end. Could you make a minimal reproduction for the issue please?

@Bekacru commented on GitHub (Oct 21, 2025): I couldn't reproduce this from my end. Could you make a minimal reproduction for the issue please?
Author
Owner

@Pirulax commented on GitHub (Oct 21, 2025):

Just to confirm, I've got the same issue... I'll try to figure out what causes it.
Edit:
Here's my tsconfig.json:

{
  "compilerOptions": {
    "types": ["node", "jest", "@testing-library/jest-dom"],
    "target": "es2022",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "paths": {
      "@/*": ["./src/*"]
    },
    "plugins": [{ "name": "next" }]
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "src/env.mjs"],
  "exclude": ["node_modules"]
}

Edit:
I've also got a bunch of different errors related to some inferred fields. I've the following in my auth.ts:

organizationPlugin({
      ac: orgAc,
      roles: orgRoles,
      schema: {
        organization: {
          additionalFields: {
            createdById: {
              type: 'string',
              required: true,
              references: { model: 'user', field: 'id', onDelete: 'set null' },
            },
            /** Not an additional field per-se, but we need it to be unique */
            name: {
              type: 'string',
              required: true,
              unique: true,
            },
          },
        },
      },
    }),

This name field overwrites the original field, because I need it to be unique (I don't know of a better solution, but it worked before 1.3.24 just fine).
Now, because of this field I get type errors when trying to call await auth.api.createOrganization:

src/lib/trpc/server/routes/org.ts:67:11 - error TS2322: Type 'string' is not assignable to type 'never'.

67           name: input.name,
             ~~~~

src/lib/trpc/server/routes/org.ts:68:11 - error TS2322: Type 'string' is not assignable to type 'never'.

68           slug: (input.slug ?? input.name.toLowerCase()).replace(/\s+/g, '-'),
             ~~~~

src/lib/trpc/server/routes/org.ts:69:11 - error TS2322: Type 'string' is not assignable to type 'never'.

69           createdById: ctx.user.id,  

For the following code:

      const data = await auth.api.createOrganization({
        headers: ctx.headers,
        body: {
          name: input.name, // Line 67
          slug: (input.slug ?? input.name.toLowerCase()).replace(/\s+/g, '-'), // Line 68
          createdById: ctx.user.id, // Line 69
        }
      });

So it seems fields from additionalFields passed to createOrganization and in my case authClient.signUp.email (As I've got a custom invite field for it) cause type errors.

@Pirulax commented on GitHub (Oct 21, 2025): Just to confirm, I've got the same issue... I'll try to figure out what causes it. Edit: Here's my `tsconfig.json`: ```json { "compilerOptions": { "types": ["node", "jest", "@testing-library/jest-dom"], "target": "es2022", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", "incremental": true, "paths": { "@/*": ["./src/*"] }, "plugins": [{ "name": "next" }] }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "src/env.mjs"], "exclude": ["node_modules"] } ``` Edit: I've also got a bunch of different errors related to some inferred fields. I've the following in my `auth.ts`: ```ts organizationPlugin({ ac: orgAc, roles: orgRoles, schema: { organization: { additionalFields: { createdById: { type: 'string', required: true, references: { model: 'user', field: 'id', onDelete: 'set null' }, }, /** Not an additional field per-se, but we need it to be unique */ name: { type: 'string', required: true, unique: true, }, }, }, }, }), ``` This `name` field overwrites the original field, because I need it to be unique (I don't know of a better solution, but it worked before 1.3.24 just fine). Now, because of this field I get type errors when trying to call `await auth.api.createOrganization`: ``` src/lib/trpc/server/routes/org.ts:67:11 - error TS2322: Type 'string' is not assignable to type 'never'. 67 name: input.name, ~~~~ src/lib/trpc/server/routes/org.ts:68:11 - error TS2322: Type 'string' is not assignable to type 'never'. 68 slug: (input.slug ?? input.name.toLowerCase()).replace(/\s+/g, '-'), ~~~~ src/lib/trpc/server/routes/org.ts:69:11 - error TS2322: Type 'string' is not assignable to type 'never'. 69 createdById: ctx.user.id, ``` For the following code: ```ts const data = await auth.api.createOrganization({ headers: ctx.headers, body: { name: input.name, // Line 67 slug: (input.slug ?? input.name.toLowerCase()).replace(/\s+/g, '-'), // Line 68 createdById: ctx.user.id, // Line 69 } }); ``` So it seems fields from `additionalFields` passed to `createOrganization` and in my case `authClient.signUp.email` (As I've got a custom `invite` field for it) cause type errors.
Author
Owner

@Pirulax commented on GitHub (Oct 21, 2025):

@astrodomas it seems like you're importing importing createAuthMiddleware as a type?

import type { createAuthMiddleware } from "better-auth/plugins";

Should probably be:

import { createAuthMiddleware } from "better-auth/plugins";

This isn't where your issue stems from most likely, as then the error wouldn't be the same as mine.

@Pirulax commented on GitHub (Oct 21, 2025): @astrodomas it seems like you're importing importing `createAuthMiddleware` as a type? ``` import type { createAuthMiddleware } from "better-auth/plugins"; ``` Should probably be: ``` import { createAuthMiddleware } from "better-auth/plugins"; ``` This isn't where your issue stems from most likely, as then the error wouldn't be the same as mine.
Author
Owner

@astrodomas commented on GitHub (Oct 22, 2025):

I couldn't reproduce this from my end. Could you make a minimal reproduction for the issue please?

It appears it might be something with my tsconfig.json. It might be because of module or moduleResolution.

@astrodomas commented on GitHub (Oct 22, 2025): > I couldn't reproduce this from my end. Could you make a minimal reproduction for the issue please? It appears it might be something with my `tsconfig.json`. It might be because of `module` or `moduleResolution`.
Author
Owner

@astrodomas commented on GitHub (Oct 22, 2025):

@Bekacru Accidentally closed this. So I'm thinking it can be that @better-auth/core does not support moduleResolution: 'node'? As when I changed my tsconfig.json settings to nodenext it works fine. So I'm not sure if it's a regression or not because the library was working with this moduleResolution on better-auth@1.3.19.

NOTE: I've fixed this issue by changing moduleResolution & module to nodenext.

@astrodomas commented on GitHub (Oct 22, 2025): @Bekacru Accidentally closed this. So I'm thinking it can be that `@better-auth/core` does not support `moduleResolution: 'node'`? As when I changed my `tsconfig.json` settings to `nodenext` it works fine. So I'm not sure if it's a regression or not because the library was working with this `moduleResolution` on `better-auth@1.3.19`. NOTE: I've fixed this issue by changing `moduleResolution` & `module` to `nodenext`.
Author
Owner

@nktnet1 commented on GitHub (Oct 22, 2025):

@astrodomas can reproduce, and can confirm that moduleResolution: node was the issue.

Changing to bundler fixed it for me.

@nktnet1 commented on GitHub (Oct 22, 2025): @astrodomas can reproduce, and can confirm that `moduleResolution: node` was the issue. Changing to `bundler` fixed it for me.
Author
Owner

@himself65 commented on GitHub (Oct 23, 2025):

This should be fixed in https://github.com/better-auth/better-auth/pull/5427.

Will land in the next stable release

@himself65 commented on GitHub (Oct 23, 2025): This should be fixed in https://github.com/better-auth/better-auth/pull/5427. Will land in the next stable release
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2168