Skip to content
Source
Sign inSign up
TanStack/query

Types issue with useQueries #3483

Send to my agent
ClosedTkDodo opened this issue 6 comments
TkDodocommented

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?

  1. TkDodo added labelsbug

  2. TkDodo added labelstypes

  3. TkDodo added labelsv4

  4. 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

  5. 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

  6. TkDodocommented

    Happy to look into it later unless that's intentional

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

  7. TkDodo closed this

  8. 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?

  9. 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.

Sign in to join this conversation.