From ae781091186f321b4e4ec9e84f64b6e4d5ea1043 Mon Sep 17 00:00:00 2001 From: Maxwell <145994855+ping-maxwell@users.noreply.github.com> Date: Thu, 16 Jul 2026 16:55:23 +1000 Subject: [PATCH] fix(client): preserve null in useSession().data type with throw:true (#9787) --- .changeset/seven-sides-report.md | 5 + .../better-auth/src/client/client.test.ts | 93 +++++++++++++++++++ .../better-auth/src/client/solid/index.ts | 4 +- .../better-auth/src/client/svelte/index.ts | 4 +- packages/better-auth/src/client/vanilla.ts | 4 +- packages/better-auth/src/client/vue/index.ts | 4 +- 6 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 .changeset/seven-sides-report.md diff --git a/.changeset/seven-sides-report.md b/.changeset/seven-sides-report.md new file mode 100644 index 0000000000..0f570b5f02 --- /dev/null +++ b/.changeset/seven-sides-report.md @@ -0,0 +1,5 @@ +--- +"better-auth": patch +--- + +Fixes an issue where `useSession({ throw: true })` incorrectly excluded `null` from its `data` type. diff --git a/packages/better-auth/src/client/client.test.ts b/packages/better-auth/src/client/client.test.ts index 2c42a8aec7..b3aff2f52a 100644 --- a/packages/better-auth/src/client/client.test.ts +++ b/packages/better-auth/src/client/client.test.ts @@ -393,6 +393,99 @@ describe("type", () => { >(); }); + /** + * @see https://github.com/better-auth/better-auth/issues/9781 + */ + describe("useSession().data with nullable types", () => { + it("useSession().data should be nullable with `throw:true` - react", () => { + const client = createReactClient({ + plugins: [testClientPlugin()], + baseURL: "http://localhost:3000", + fetchOptions: { + throw: true, + customFetchImpl: async (url, init) => { + return new Response(); + }, + }, + }); + + type UseSessionReturn = ReturnType; + type DataField = UseSessionReturn["data"]; + expectTypeOf().toMatchTypeOf(); + }); + + it("useSession().data should be nullable with `throw:true` - vue", () => { + const client = createVueClient({ + plugins: [testClientPlugin()], + baseURL: "http://localhost:3000", + fetchOptions: { + throw: true, + customFetchImpl: async (url, init) => { + return new Response(); + }, + }, + }); + + const session = client.useSession(); + type DataField = (typeof session)["value"]["data"]; + expectTypeOf().toMatchTypeOf(); + }); + + it("useSession().data should be nullable with `throw:true` - solid", () => { + const client = createSolidClient({ + plugins: [testClientPlugin()], + baseURL: "http://localhost:3000", + fetchOptions: { + throw: true, + customFetchImpl: async (url, init) => { + return new Response(); + }, + }, + }); + + type UseSession = ReturnType; + type UseSessionReturn = ReturnType; + type DataField = UseSessionReturn["data"]; + expectTypeOf().toMatchTypeOf(); + }); + + it("useSession().data should be nullable with `throw:true` - svelte", () => { + const client = createSvelteClient({ + plugins: [testClientPlugin()], + baseURL: "http://localhost:3000", + fetchOptions: { + throw: true, + customFetchImpl: async (url, init) => { + return new Response(); + }, + }, + }); + + type UseSessionAtom = ReturnType; + type UseSessionReturn = ReturnType; + type DataField = UseSessionReturn["data"]; + expectTypeOf().toMatchTypeOf(); + }); + + it("useSession().data should be nullable with `throw:true` - vanilla", () => { + const client = createVanillaClient({ + plugins: [testClientPlugin()], + baseURL: "http://localhost:3000", + fetchOptions: { + throw: true, + customFetchImpl: async (url, init) => { + return new Response(); + }, + }, + }); + + type UseSessionAtom = typeof client.useSession; + type UseSessionReturn = ReturnType; + type DataField = UseSessionReturn["data"]; + expectTypeOf().toMatchTypeOf(); + }); + }); + it("should infer `error` schema correctly", async () => { const client = createSolidClient({ plugins: [testClientPlugin()], diff --git a/packages/better-auth/src/client/solid/index.ts b/packages/better-auth/src/client/solid/index.ts index 939c22a4cb..3ca4f1126f 100644 --- a/packages/better-auth/src/client/solid/index.ts +++ b/packages/better-auth/src/client/solid/index.ts @@ -51,9 +51,7 @@ type ClientSession