-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
feat: type-safe way to skip/disable useQuery #4252
Comments
The solutions you’ve come up with are correct. If you think there’s a better way you should raise it with the react-query GitHub because we just wrap it and don’t add additional functionality which they don’t already provide |
I believe this is an issue with how tRPC abstracts the react-query API by asserting the type of the A raw react-query example could use the lack of ts array bounds checking which still has the issue of duplicating the check but does not have to use the const param: string | null
useQuery({
queryKey: param ? [param] : [],
queryFn: ({ queryKey }) => fetchSomething(queryKey[0]),
enabled: !!param
}) |
Typescript has no idea at compile time whether a procedure is enabled or disabled, because that is assumed to be changing at run-time. Therefore there is no way to validate that the variables being passed to the input are valid at compile time either. The input won't be used when disabled, so either it has to be faked with some dummy/placeholder data or a There's nothing AFAIK which tRPC can do here, it's an application choice of how to work within the type system. If you have a strong belief it can be improved then PRs are welcome of course 🙂 |
I am not a typescript expert, but I wonder if it would be possible to do something like interface IUseQuery<Input, Options, Output> {
(input: Options extends { enabled: false } ? unknown : Input, options: Options): Output
} |
Not possible in a nice way AFAIK. The underlying reason is that React doesn't support conditional hooks. |
Describe the feature you'd like to request
It would be useful to be able to skip a query in a type-safe way.
Currently, using the
enabled
option will cause type-checking to fail likeDescribe the solution you'd like to see
rtk-query
has the concept of askipToken
on theiruseQuery
which is a special object that allows the input arg to pass the type checking and disables the query (docs link).Describe alternate solutions
Some alternate solutions
any
to bypass type-checking - requires keeping both args in syncuseQueries
- not the most ergonomic and seems to be discouragedAdditional information
Discord discussion: https://discord.com/channels/867764511159091230/1100458301281550457/1100458301281550457
👨👧👦 Contributing
The text was updated successfully, but these errors were encountered: