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