-
-
Notifications
You must be signed in to change notification settings - Fork 528
Wrong type in openapi-react-query when using select to transform data #1845
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
Comments
Thanks for filing! You’re right—we should add a template for openapi-react-query. Though for now, it’s not an issue opening a bug against openapi-fetch because there’s still overlap in what those 2 libraries do. |
Hi! This is definitely a bug and could be easily fixed by changing the types. Happy to review a PR for that! |
I'll have a look at it 👍 |
It seems, the only (pragmatic) way to make the return value infer the same value as returned by the My understanding is the following: In the current implementation, the response of
UseQueryOptions ). I could not come up with an easy approach to extract TData in the current implementation.
I recently tried to build a wrapper for tanstack query myself and also ran into the problem that the return value of function TanstackQueryWrapper<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
{queryOptions}: { queryOptions: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>;}
) {
const { data } = useQuery(queryOptions) // Data is inferred correctly
} From my current POV there is more to do here than changing types. Happy to hear more opinions on this and how it could be solved without too much refactoring. Edit: Maybe also interesting concerning the topic https://twitter.com/TkDodo/status/1491451513264574501 |
Thanks for the detailed answer, this definitely needs to be addressed. I'll try to find some time in the following days to improve the types of the libraries and use more properly the @tanstack/react-query (and hopefully fix this issue). As you are already advanced on this subject, I am totally open to review a PR that fixes this issue. |
@kerwanp opened a MR. Let's call it a "solution oriented" approach :) I guess leveraging the "native" tanstack query type system would be the cleaner solution but I think this is not possible without making significant changes to the current implementation. |
Mind: This is a bug in openapi-react-query. There is no template for opening Bugs for the openapi-react-query package yet.
Description
The
select
transformer in thequeryOptions
of the useQuery method expects a value of typedata
as return value. The value should be transformable to an arbitrary value (and be inherited correctly). Example:Cool:
Not cool:
Reproduction
See the implementation at the bottom of
fetchClient.ts
in this Stackblitz.Expected result
Arbitrary return values in the
select
method do not throw type errors. Furthermore,data
should infer the type of the returned value of theselect
method (if set):The text was updated successfully, but these errors were encountered: