From 08fa1f89c39bd8ef71d98b845b5bfafdb2a4595c Mon Sep 17 00:00:00 2001 From: Fraol Lemecha Date: Fri, 12 Sep 2025 21:36:55 +0300 Subject: [PATCH] fix: should infer types correctly when empty list of plugins is provided (#4612) --- packages/better-auth/src/db/field.ts | 26 ++++++++++--------- packages/better-auth/src/test-utils/index.ts | 2 +- .../src/test-utils/test-instance.ts | 2 +- packages/better-auth/src/types/options.ts | 2 +- packages/better-auth/src/types/types.test.ts | 22 ++++++++++++++++ 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/packages/better-auth/src/db/field.ts b/packages/better-auth/src/db/field.ts index 41c77abf..b66cef58 100644 --- a/packages/better-auth/src/db/field.ts +++ b/packages/better-auth/src/db/field.ts @@ -274,19 +274,21 @@ export type InferFieldsFromPlugins< Options extends BetterAuthOptions, Key extends string, Format extends "output" | "input" = "output", -> = Options["plugins"] extends Array - ? T extends { - schema: { - [key in Key]: { - fields: infer Field; +> = Options["plugins"] extends [] + ? {} + : Options["plugins"] extends Array + ? T extends { + schema: { + [key in Key]: { + fields: infer Field; + }; }; - }; - } - ? Format extends "output" - ? InferFieldsOutput - : InferFieldsInput - : {} - : {}; + } + ? Format extends "output" + ? InferFieldsOutput + : InferFieldsInput + : {} + : {}; export type InferFieldsFromOptions< Options extends BetterAuthOptions, diff --git a/packages/better-auth/src/test-utils/index.ts b/packages/better-auth/src/test-utils/index.ts index 2f00866c..fc81a009 100644 --- a/packages/better-auth/src/test-utils/index.ts +++ b/packages/better-auth/src/test-utils/index.ts @@ -93,7 +93,7 @@ export async function getTestInstanceMemory< ...options?.advanced, }, plugins: [bearer(), ...(options?.plugins || [])], - } as O extends undefined ? typeof opts : O & typeof opts); + } as unknown as O extends undefined ? typeof opts : O & typeof opts); const testUser = { email: "test@test.com", diff --git a/packages/better-auth/src/test-utils/test-instance.ts b/packages/better-auth/src/test-utils/test-instance.ts index 07de0c25..db15c049 100644 --- a/packages/better-auth/src/test-utils/test-instance.ts +++ b/packages/better-auth/src/test-utils/test-instance.ts @@ -109,7 +109,7 @@ export async function getTestInstance< ...options?.advanced, }, plugins: [bearer(), ...(options?.plugins || [])], - } as O extends undefined ? typeof opts : O & typeof opts); + } as unknown as O extends undefined ? typeof opts : O & typeof opts); const testUser = { email: "test@test.com", diff --git a/packages/better-auth/src/types/options.ts b/packages/better-auth/src/types/options.ts index b5c686b9..df01fb11 100644 --- a/packages/better-auth/src/types/options.ts +++ b/packages/better-auth/src/types/options.ts @@ -291,7 +291,7 @@ export type BetterAuthOptions = { /** * List of Better Auth plugins */ - plugins?: BetterAuthPlugin[]; + plugins?: [] | BetterAuthPlugin[]; /** * User configuration */ diff --git a/packages/better-auth/src/types/types.test.ts b/packages/better-auth/src/types/types.test.ts index f1c36001..4107c5c5 100644 --- a/packages/better-auth/src/types/types.test.ts +++ b/packages/better-auth/src/types/types.test.ts @@ -55,4 +55,26 @@ describe("general types", async (it) => { activeOrganizationId?: string | undefined | null; }>(); }); + + it("should infer the same types for empty plugins and no plugins", async () => { + const { auth: authWithEmptyPlugins } = await getTestInstance({ + plugins: [], + secret: "test-secret", + emailAndPassword: { + enabled: true, + }, + }); + + const { auth: authWithoutPlugins } = await getTestInstance({ + secret: "test-secret", + emailAndPassword: { + enabled: true, + }, + }); + + type SessionWithEmptyPlugins = typeof authWithEmptyPlugins.$Infer; + type SessionWithoutPlugins = typeof authWithoutPlugins.$Infer; + + expectTypeOf().toEqualTypeOf(); + }); });