Skip to content

[Types] useMutation returning union undefined with new types? #810

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

Closed
Buuntu opened this issue Jul 29, 2020 · 7 comments
Closed

[Types] useMutation returning union undefined with new types? #810

Buuntu opened this issue Jul 29, 2020 · 7 comments

Comments

@Buuntu
Copy link

Buuntu commented Jul 29, 2020

Describe the bug
Mutate now potentially returns an undefined type, which is different from the return type before I believe? Maybe that's the expected behavior but by looking at the docs you generally aren't checking that something is returned. Shouldn't it just throw an error rather than return undefined if something goes wrong?

So using the new types you now have to check that the data exists before using it.

Before

const [mutate] = useMutation(mutateFunction);

try {
  const data = await mutate();
  // do something
} catch (err) {
// error handling
}

After:

const [mutate] = useMutation(mutateFunction);

try {
  const data = await mutate();
  if (data) {
    // do something
  }
} catch (err) {
// error handling
}

There might be a good reason for this but it does deviate from the docs (https://react-query.tanstack.com/docs/guides/mutations#basic-mutations) and previous types.

@MichaelDeBoey
Copy link
Collaborator

Is this still a valid problem now #767 is merged? 🤔

@Buuntu
Copy link
Author

Buuntu commented Jul 29, 2020

Is this still a valid problem now #767 is merged? 🤔

I just installed it today so I think so? WIth:

npm i react-query@tsnext

My package.json:

"react-query": "^2.5.7-tsnext.1",

@boschni
Copy link
Collaborator

boschni commented Jul 30, 2020

Hi @Buuntu, thanks for giving feedback on the new types! The type has indeed changed because the mutation returns undefined when an error occurs and the throwOnError option was not set to true. The documentation does seem to imply that the mutate function will throw by default because of the try catch in the example. This could either be a bug in the code or in the docs. What should be the default behaviour here @tannerlinsley ?

@tannerlinsley
Copy link
Collaborator

Mutate does not throw by default unless throwOnError is true. The docs should be updated to reflect this.

@Buuntu
Copy link
Author

Buuntu commented Jul 30, 2020

I suppose it's not doable to set a conditional type depending on what the config is? Something like: https://www.typescriptlang.org/docs/handbook/advanced-types.html#conditional-types.

If not I'm happy to update the docs and submit a PR.

@quantizor
Copy link

Is this still a valid problem now #767 is merged?

Yes it is, I get the error on 2.5.11 but not on 2.5.7-tsnext.1

@tannerlinsley
Copy link
Collaborator

Looks like with the docs and the new 2.5.7-tsnext.1 types, we'll be in good shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants