Skip to content

useMutation types: variables are optional #1077

@TkDodo

Description

@TkDodo

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions