Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type for useQuery error too optimistic? #2151

Open
ludwighogstrom opened this issue Feb 12, 2025 · 1 comment
Open

Type for useQuery error too optimistic? #2151

ludwighogstrom opened this issue Feb 12, 2025 · 1 comment
Labels
bug Something isn't working openapi-react-query Relevant to openapi-react-query

Comments

@ludwighogstrom
Copy link

ludwighogstrom commented Feb 12, 2025

openapi-react-query version

0.3.0

Description

The type of the error returned by useQuery and queryOptions is determined by the "paths" specification. For example:

const {data, error} = useQuery(queryClient.queryOptions("get", "/my-path"));

In this case, error is typed as:

{ error_code: number } | null

That object matches the API response when an expected error occurs.

However, if something unexpected goes wrong—such as fetch throwing TypeError: Failed to fetch—the typing no longer aligns. In this case, the error is not an object with an error_code property but a FetchError instead.

An unexpected response can also occur if the API is temporarily down and fails to return a response in the expected format.

Reproduction

Block the request in Chrome DevTools and log the error. You'll see that the error is a TypeError: Failed to fetch, rather than the expected format defined by the API specification.

Instruction how to block request https://developer.chrome.com/docs/devtools/network-request-blocking

Expected result

The typing of error should be something else. Maybe unknown?

@ludwighogstrom ludwighogstrom added bug Something isn't working openapi-react-query Relevant to openapi-react-query labels Feb 12, 2025
@ludwighogstrom
Copy link
Author

It seems that TanStack Query recommends keeping the error type as an Error (default behavior). https://tanstack.com/query/v5/docs/framework/react/typescript#typing-the-error-field

However, this has the drawback that type inference for all other generics of useQuery will not work anymore. It is generally not considered a good practice to throw something that isn't an Error...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-react-query Relevant to openapi-react-query
Projects
None yet
Development

No branches or pull requests

1 participant