Skip to content

fix: add client response type inference when options.throw is true#6

Open
ledouxm wants to merge 1 commit intobetter-auth:mainfrom
ledouxm:main
Open

fix: add client response type inference when options.throw is true#6
ledouxm wants to merge 1 commit intobetter-auth:mainfrom
ledouxm:main

Conversation

@ledouxm
Copy link
Copy Markdown

@ledouxm ledouxm commented Feb 18, 2025

Hello !
I had an issue with the return type of the better-fetch client when using { throw: true } :

const api = createClient<AppRouter>({
  throw: true,
});


const api2 = createClient<AppRouter>({
  throw: false,
});

const resp1 = await api("/is-first-connection", {});
const resp2 = await api2("/is-first-connection", {});

type AreResponsesEqual = typeof resp1 extends typeof resp2 ? true : false;
//   ^? type AreResponsesEqual = true

So I've used function overloading to fix it.

If you think of a better way of achieving this, please let me know :)

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Iulian-Dragomirescu
Copy link
Copy Markdown

This issue still persists in version 2.0.2

When using:

const res = await apiClient("/v1/quiz/categories"); the returned type is inferred as: res.data?.ok (and the data is also correctly under res.data)

However, when using:

const res = await apiClient("/v1/quiz/categories", { throw: true }); the inferred type is still: res.data?.ok
but the actual data is returned as: res.ok

So the type is not correctly inferred when throw is set to true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants