question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useMutation types: variables are optional

See original GitHub issue

Describe the bug

in TypeScript, the mutation function created by the useMutation hook can always be called without passing arguments, because the type TVariables is set to optional:

https://github.com/tannerlinsley/react-query/blob/00b9e968a80ca7c51fd50479aee852bb41bf9bc6/src/react/useMutation.ts#L132

To Reproduce

I made a codeSandbox example: https://codesandbox.io/s/async-currying-wepni

Please look at the comment at line 54

I should be forced to call that function like this:

mutatePostTodo(text)

but I can also call it like this:

mutatePostTodo()

Expected behavior

The type of variables that you need to pass to that function should be equal to the type that the actual mutation function accepts, in other words, it should be variables: TVariables rather than variables?: TVariables

Desktop (please complete the following information):

  • OS: MacOS Catalina
  • Browser: Chrome
  • Version: 85

Additional context

All the usages of variables in that function are accessed with the bang operator, for example:

https://github.com/tannerlinsley/react-query/blob/93db5b61246dcd381d699054de48fca685bef73b/src/react/useMutation.ts#L146

or https://github.com/tannerlinsley/react-query/blob/00b9e968a80ca7c51fd50479aee852bb41bf9bc6/src/react/useMutation.ts#L174

So I don’t really see why it should be optional to begin with 🤷 .

Happy to make a PR if you approve of this 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13

github_iconTop GitHub Comments

1reaction
boschnicommented, Sep 30, 2020

The typing issue has been fixed in V3

1reaction
tannerlinsleycommented, Sep 24, 2020

I would be okay implementing this in v2 as non-breaking, but I’ll leave that up to @boschni since he knows more about the type implications. Otherwise, expect it in v3.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useMutation types: variables are optional · Issue #1077 - GitHub
I have a mutation function that has parameters which are all optional and there are defaults. When I have no parameters typescript does...
Read more >
Mutations in Apollo Client - Apollo GraphQL Docs
Here, we use the variables option to provide the values of any GraphQL variables that our mutation requires (specifically, the type of the...
Read more >
useMutation | TanStack Query Docs
The mutation function you can call with variables to trigger the mutation and optionally override options passed to useMutation . variables: TVariables.
Read more >
How to declare useMutation return type in 'react-query'
With the types that I put on api and setFriendCode , I get <Response, unknown, Params, unknown> . const object = useMutation( setFriendCode...
Read more >
useMutation | Relay
[Optional] A function with the same signature as commitMutation , which will be called in its stead. Defaults to commitMutation . Flow Type ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found