-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
MutationFunction optional parameters #4264
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
plase make sure to save the sandbox. It's empty when I open it! |
so this works:
https://codesandbox.io/s/thirsty-andras-iw67ud?file=/src/App.tsx:105-125 |
Thanks for your reply, yeah, |
honestly, at this point i'm not sure what's the difference between declaring a function parameter as optional and declaring a function parameter as void function fa(a?: number) {
return a;
}
function fb(a: number | void) {
return a;
}
fa(); // ok
fa(undefined); // ok
fb(); // ok
fb(undefined); // ok |
yeah I get that. I think right now, query/packages/react-query/src/types.ts Line 100 in f715b30
The function is then constructed like so: query/packages/react-query/src/types.ts Lines 102 to 104 in f715b30
I think mutations without variables are the exception - a DELETE request comes to mind. I haven't seen one with only one optional parameter though. What you can do is wrap and unwrap it in an object if you can't change the type of the function that make the request itself:
see: https://codesandbox.io/s/summer-monad-w3isyh?file=/src/App.tsx it takes away the option to just call Otherwise, if you or someone else wants to take a stab at improving the types, PRs are welcome. |
I tried something like this: export type MutateFunction<
TData = unknown,
TError = unknown,
TVariables = void,
TContext = unknown,
> = TVariables extends undefined ? {
(
variables?: TVariables,
options?: MutateOptions<TData, TError, void, TContext>,
): Promise<TData>
} : {
(
variables: TVariables,
options?: MutateOptions<TData, TError, TVariables, TContext>,
): Promise<TData>
} But got few type errors in other files. i'm just get started with react-query and don't know how to resolve those type errors. @TkDodo thanks for you quick reply ❤️ |
intuitively,
the statement may not be true when building a library that uses |
This would need overloads and runtime checks that we cannot do because we are not in control of the variables. What would a call to:
be at runtime? Is this no variables + options, or is it just variables, no options? It's ambiguous. For useQuery, the overloads only work because we know that the key is always an array, and that the queryFn is always a function, and that options are always an object (at runtime). And still, we want to move away from overloads. So no, I don't see a good way to make that work. |
Describe the bug
useMutation
does't not persist the signature ofMutationFunction
. is it make sense and possible to persist MutationFunction optional parameters in muration.murate?feel redundant that i have to write
mutation.mutate(undefined);
instead of justmutation.mutate()
.See also
Treating undefined parameters as optional
Your minimal, reproducible example
https://codesandbox.io/s/zen-fire-9iu1er?file=/src/App.tsx
Steps to reproduce
--
Expected behavior
See above
How often does this bug happen?
No response
Screenshots or Videos
No response
Platform
react-query version
4.9.0
TypeScript version
4.7.4
Additional context
No response
The text was updated successfully, but these errors were encountered: