Skip to content

Types issue with useQueries #3483

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
TkDodo opened this issue Apr 8, 2022 · 6 comments
Closed

Types issue with useQueries #3483

TkDodo opened this issue Apr 8, 2022 · 6 comments
Labels
bug Something isn't working types v4

Comments

@TkDodo
Copy link
Collaborator

TkDodo commented Apr 8, 2022

useQueries type includes Promise in the inferred type of .data, which is not renderable as React.ReactNode with the new react 18 typings.

failing test on beta:

  it('infers returned Promises', async () => {
    const key1 = queryKey()

    // @ts-expect-error (Page component is not rendered)
    // eslint-disable-next-line
    function Page() {
      const result = useQueries({
        queries: [
          { queryKey: key1, queryFn: () => Promise.resolve(1)}
        ]
      })

      expectType<number | undefined>(result[0].data)
    }
  })

fails with:

TS2345: Argument of type 'number | Promise<number> | undefined' is not assignable to parameter of type 'number | undefined'.   Type 'Promise<number>' is not assignable to type 'number'.

@artysidorenko could you take a look at this please?

@artysidorenko
Copy link
Contributor

Heya, had a look, added a PR to fix. Wasn't so much react-18 related; previously it had been correctly getting the Promise<string> => string inference, but got tripped up by this union added in a recent PR

@artysidorenko
Copy link
Contributor

artysidorenko commented Apr 10, 2022

Btw, I noticed something else along the way. My understanding from reading your other issue is that you want to warn with a type error anytime queryFn resolves to undefined?

I think at the moment (beta branch) it only works when it's synchronous undefined but not Promise<undefined>

Errors as expected

// @ts-expect-error
useQuery(key, () => undefined)

This doesn't complain

useQuery(key, async () => undefined)

Happy to look into it later unless that's intentional

@TkDodo
Copy link
Collaborator Author

TkDodo commented Apr 11, 2022

Happy to look into it later unless that's intentional

oh yes good catch. Promise<void> should also error on v4. Thank you 🙏

@TkDodo TkDodo closed this as completed Apr 11, 2022
@TkDodo
Copy link
Collaborator Author

TkDodo commented Apr 11, 2022

just to be sure, @artysidorenko, the async () => undefined issue wasn't fixed with #3493 right?

from what I can see:

  • useQuery(key, () => undefined) errors, as expected ✅
  • useQuery(key, () => undefined) no error ❌
  • useQueries({ queries: [{ queryKey: key, queryFn: () => undefined }]}) no error ❌
  • useQueries({ queries: [{ queryKey: key, queryFn: async () => undefined }]}) no error ❌

should I create a separate issue for that?

@artysidorenko
Copy link
Contributor

Indeed, it wasn't fixed in #3493 , planning to look into it next. If it helps from a tracking pov, can use a separate issue, sure.

@TkDodo
Copy link
Collaborator Author

TkDodo commented Apr 15, 2022

follow up created: #3516

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working types v4
Projects
None yet
Development

No branches or pull requests

2 participants