Skip to content

TypeScript error in mutate function from useMutation hook for variables param #2134

Closed

Description

@ofilipowicz

Describe the bug
When using mutate function from useMutation hook I'm getting TypeScript error for param variables

Argument of type '{ password: string; email: string; }' is not assignable to parameter of type 'void'.

To Reproduce
Steps to reproduce the behavior:

  const { mutate } = useMutation((values) =>
    axios.post('/test', values),
  );
  const onSubmit = () => {
    mutate({ password: 'test', email: 'test' });
  };

Expected behavior
Looks like type TVariables = void in UseMutateFunction is causing the issues. It should accept objects like in the example from docs https://react-query.tanstack.com/guides/mutations

Additional context
react-query@3.13.9
typescript@4.2.4

Activity

  1. TkDodo commented on Apr 16, 2021

    @TkDodo
    Collaborator

    you very likely need to type the input to the muateFn, otherwise, typescript has no way of knowing what it is:

    type Variables = { password: string, email:string }
    const { mutate } = useMutation((values: Variables) =>
        axios.post('/test', values),
    );
     const onSubmit = () => {
        mutate({ password: 'test', email: 'test' });
     };
    
  2. YizhiHong commented on May 12, 2021

    @YizhiHong

    ^ it doesn’t work at all. if you checkout the error above. it's saying
    { password: string, email:string } is not assignable to void. it means the mutate function should not have param at all.

  3. TkDodo commented on May 12, 2021

    @TkDodo
    Collaborator

    that is just because if you write:

      const { mutate } = useMutation((values) =>
        axios.post('/test', values),
      );
    

    it is not defined what values should have for a type. The generic TVariables defaults to void and that's the error you are getting. If you write a function in typescript:

    const myFunction = (variables) => console.log(variables)
    

    the compiler needs to know what variables will be, otherwise, it will be implicitly any.

    if you take my example where values has a type of values: Variables, you will see that it works.

  4. SevenOutman commented on Mar 5, 2022

    @SevenOutman

    What should I pass as the first argument to mutate if the mutation doesn't take any variables?

    function useLogout() {
      return useMutation(() => api.logout());
    }
    
    const logout = useLogout();
    
    logout.mutate(
      null, // neither null or {} is not assignable to parameter of type 'void'.
      {
        // ...options
      }
    )

    Update

    Found that void 0 does the trick.

    logout.mutate(
      void 0,
      {
        // ...options
      }
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions