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 checking select works differently on useQueries compared to useQuery #6556

Open
jtannas opened this issue Dec 19, 2023 · 6 comments
Open
Labels

Comments

@jtannas
Copy link

jtannas commented Dec 19, 2023

Describe the bug

When using useQuery, TS correctly infers the argument type of the select query option to be the return type of the queryFn data.
Applying the same settings to useQueries it loses that link and changes it to any.
The linked TS sandbox shows the issue better than I can explain it though.

Your minimal, reproducible example

https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1WO1AGhQ21wE84BfOAMyghDgCIABGAQwDtVOBjANYB6KOg58YAWgCOOKOWYAoRXwg94IcgBEOnOAF44AbQCMxAEzEAzMQAsxAKzEAbAF1lKtbziay8gyRYcuQAFAiKcHCyFADS6OQAXMYA5OgAbuhcya6EEVHBAGJcSSEAlAYAfHAACgwgwBgAdKKoEAA2GSGaOpyluZEYbegSJRxQUOX6VWNQjbTAbTC4IcCVcKsApHDmBvqGAAx9ilSlyqrqPuR++KgBaEG4N2HReARJRnnhkZEv5HGJKXSmWy-W+vyKJUmVVqjAa6GaBHanW6ug4R2+cEGwxgo3GULgMzmCyWUBWa02212B3R1FyrhOiiAA

Steps to reproduce

See linked Typescript playground to a minimal reproducible example

Expected behavior

I would like the type checking of select on useQueries to work as well as it does on useQuery.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • Ubuntu 22.04

Tanstack Query adapter

react-query

TanStack Query version

v5.3.2

TypeScript version

v5.14.0

Additional context

No response

@jtannas jtannas changed the title Type checking select works differently on useQueries compared to useQueries Type checking select works differently on useQueries compared to useQuery Dec 19, 2023
@TkDodo
Copy link
Collaborator

TkDodo commented Dec 19, 2023

I think it's a known limitation.

Possible workarounds:

@judicaelandria
Copy link
Contributor

was gonna propose the first workarounds but the second one seems fine too

@TkDodo TkDodo added types known limitation help wanted Extra attention is needed labels Dec 19, 2023
@TkDodo
Copy link
Collaborator

TkDodo commented Dec 19, 2023

I'll leave this open if someone wants to take a look, but given that we have good workarounds, it's not a priority for me.

@jtannas
Copy link
Author

jtannas commented Dec 19, 2023

Thanks for the tips :)
I've been annotating the inputs to work around it but was shying away from queryOptions because these are suspense queries. I tried some workarounds with satisfies UseSuspenseQueryOptions to narrow it down, but no luck there.

Any suggestions there? Maybe a suspenseQueryOptions helper?

@TkDodo
Copy link
Collaborator

TkDodo commented Dec 19, 2023

You can pass queryOptions just fine to useSuspenseQueries

playground

@jtannas
Copy link
Author

jtannas commented Dec 19, 2023

yep - was just hoping for something that'd prevent people slipping in placeholderData/enabled since that caught us pretty badly when we started transitioning to suspense

manudeli added a commit to toss/suspensive that referenced this issue Apr 4, 2024
fix #767 with @wes5510

@wes5510 I guess that this issue is simmilar issue with
TanStack/query#6556
I want to solve this issue like
TanStack/query#6556 (comment)
(Is this solution same with your opinion?). In my opinion, I think it
would be a good idea to add queryOptions

![Apr-03-2024
01-54-47](https://github.com/suspensive/react/assets/61593290/e767b5ad-df9b-4f05-8678-a5b7f47bd6e4)

### select type when I hover on field name

![image](https://github.com/suspensive/react/assets/61593290/8091735f-40c6-43cd-8035-8682b5c4fb96)
### select type when I hover on select's arg
I expected TData but it's any!!??🥲

![image](https://github.com/suspensive/react/assets/61593290/a8be5353-0f18-4876-bdd9-e791e1d14d70)

I mention you because you suggested this interface(queryOptions) and
I've wanted to add it for a while too. I would like to request a review
to you with our maintainers.

# Solution: new `queryOptions`

This new queryOptions will solve select's type issue like
TanStack/query#6556 (comment)

## More advantages

Tkdodo, The maintainer of TanStack Query explains well why this
interface is needed in [video explaining queryOptions in TanStack Query
v5](https://youtu.be/bhE3wuB_TuA?feature=shared&t=1697).
You can also use queryOptions in TanStack Query v4.

1. QueryKey management becomes easier by processing queryKey and queryFn
together.
2. You can remove unnecessary custom query hooks. This is because they
can all be used directly in `useQuery`, `useQueries` of TanStack Query
v4, and `useSuspenseQuery`, `useSuspenseQueries`, and `SuspenseQuery` of
Suspensive React Query.
3. TanStack Query v5 already supports queryOptions. This Suspensive
React Query's `queryOptions` will make migration from TanStack Query v4
to TanStack Query v5 easier.

```jsx /queryOptions/
import { queryOptions, useSuspenseQuery, useSuspenseQueries, SuspenseQuery } from '@suspensive/react-query'
import { useQuery, useQueries, useQueryClient } from '@tanstack/react-query'

const postQueryOptions = (postId) =>
   queryOptions({
     queryKey: ['posts', postId] as const,
     queryFn: ({
       queryKey: [, postId], // You can use queryKey.
     }) => fetch(`https://example.com/posts/${postId}`),
   })

// No need to create custom query hooks.
// You can use queryOptions directly in useQuery, useQueries, useSuspenseQuery, useSuspenseQueries, SuspenseQuery.
const post1Query = useQuery(postQueryOptions(1))
const { data: post1 } = useSuspenseQuery({
   ...postQueryOptions(1);
   refetchInterval: 2000, // Extensibility is clearly expressed in usage.
})
const [post1Query, post2Query] = useQueries({
   queries: [postQueryOptions(1), { ...postQueryOptions(2), refetchInterval: 2000 }],
})
const [{ data: post1 }, { data: post2 }] = useSuspenseQueries({
   queries: [postQueryOptions(1), { ...postQueryOptions(2), refetchInterval: 2000 }],
})
const Example = () => <SuspenseQuery {...postQueryOptions(1)}>{({ data: post1 }) => <>{post1.text}</>}</SuspenseQuery>

// You can easily use queryKey and queryFn in queryClient's methods.
const queryClient = useQueryClient()
queryClient.refetchQueries(postQueryOptions(1))
queryClient.prefetchQuery(postQueryOptions(1))
queryClient.invalidateQueries(postQueryOptions(1))
queryClient.fetchQuery(postQueryOptions(1))
queryClient.resetQueries(postQueryOptions(1))
queryClient.cancelQueries(postQueryOptions(1))

```


## PR Checklist

- [x] I did below actions if need

1. I read the [Contributing
Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md)
2. I added documents and tests.

---------

Co-authored-by: Gihyeon Lee <[email protected]>
Co-authored-by: Minsoo Kim <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants