Skip to content
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

Vue reactive call no longer preserves union type of vue-query's useQuery #4556

Closed
henribru opened this issue Nov 26, 2022 · 3 comments · Fixed by #4562
Closed

Vue reactive call no longer preserves union type of vue-query's useQuery #4556

henribru opened this issue Nov 26, 2022 · 3 comments · Fixed by #4562

Comments

@henribru
Copy link
Contributor

henribru commented Nov 26, 2022

Describe the bug

In the old, separate vue-query package, using vue's reactive on the return of useQuery preserved the union return type, but now all the union types seem to get combined into one type. This means you can't narrow types based on e.g. isSuccess.

Your minimal, reproducible example

I attempted to reproduce this in the Typescript Playground, but for whatever reason it just shows me any for the return type of the old useQuery ... https://www.typescriptlang.org/play?ts=4.8.4#code/JYWwDg9gTgLgBAbzlApgQwMY2ANxXAXzgDMoIQ4AiHAVxUoG4AoUSWROGgZxQEU6oAT0IkyFAOS0UAWgCOAweOato8JNz4K4aLpx78UQgHIoA7iNLk44gAIw0AOy72MAawD0UuQqVMmxGgcsYAgHEggIAAoASkQmOATkFBgaKDCABTFgHgA6VC4IABs8SMpiCMpopgI-DFDnOHlDYQBeJMxsEo0DIUiAbTKKgF0AGnCIaOjmJqEcgBM0exZiOEiZwRzsgGUaDAwULi5YhHjE9fnFtGra+vh1k3M21A7cFEjuhQf+wYhKUfHJtMFBclsAVmtPmZNlwdnsDkc4olGpDTCCrgQgA

I've made a local reproduction you can clone instead: https://github.com/henribru/vue-query-use-query-reactive/blob/master/src/main.ts

Steps to reproduce

  1. Open up src/main.ts in e.g. VS Code
  2. Hover over query.data and notice it's been narrowed to string
  3. Hover over queryNew.data and notice it hasn't been narrowed and is string | undefined
  4. Hover over query and notice it's a union type
  5. Hover over queryNew and notice it's not a union type

Expected behavior

I expected @tanstack/vue-query to have the same behavior as vue-query so that I can discriminate between union types.

How often does this bug happen?

Every time

Screenshots or Videos

image

image

image

image

Platform

  • OS: Linux
  • Browser: N/A
  • Version: N/A

react-query version

4.17.1 for @tanstack/vue-query, 1.26.0 for vue-query

TypeScript version

4.8.4

Additional context

No response

@henribru henribru changed the title Vue reactive call no longer preserves union type of vue-query' useQuery Vue reactive call no longer preserves union type of vue-query's useQuery Nov 26, 2022
@henribru
Copy link
Contributor Author

This appears to be related to the use of Omit in the definition of UseQueryReturnType, if I just naively remove that the error goes away.

Probably related to issues like these:
microsoft/TypeScript#28339
microsoft/TypeScript#31501
microsoft/TypeScript#39556
microsoft/TypeScript#46361

I guess it could probably be fixed using a DistributiveOmit?
https://stackoverflow.com/questions/57103834/typescript-omit-a-property-from-all-interfaces-in-a-union-but-keep-the-union-s
https://davidgomes.com/pick-omit-over-union-types-in-typescript/

@TkDodo
Copy link
Collaborator

TkDodo commented Nov 26, 2022

FYI, we won't need the omit in v5 because we remove the overloads there

@henribru
Copy link
Contributor Author

henribru commented Nov 26, 2022

Sounds good. I think I'll do a PR now so it's fixed in v4 as well, though.

Edit: We might be talking about different omits though. I meant these, maybe you meant these?

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

Successfully merging a pull request may close this issue.

2 participants