Skip to content
Source
Sign inSign up
TanStack/query

fix(types): fix promise inference in useQueries #3493

Mergedartysidorenko(GitHub profile, opens in a new tab)merged 2 commits intobeta fromrefs/pull/3493/headmerged by TkDodo View on GitHub

fixes issue #3483

This new helper QueryFunctionData was introduced in the latest branch:

type QueryFunctionData<T> = T extends undefined ? never : T

type QueryFunction<
  T = unknown,
  TQueryKey extends QueryKey = QueryKey
> = (
  context: QueryFunctionContext<TQueryKey>
) => QueryFunctionData<T> | Promise<QueryFunctionData<T>>

Looks like when a conditional expression is duplicated like this in a union, it's not able to pass along any infer logic correctly.

(TS Playground link with a simplified example - not restricted to promises though... you can replace Promise<X> with Array<X> for the same effect)

This means the useQueries helper types are getting confused, thinking TQueryFnData could itself be a promise.

Interestingly, if we make it so there's only 1 instance of QueryFunctionData, with a union as its type argument, as I'm doing in this PR, it seems to work just fine. Rules of TS claim that the two expressions should be equivalent, so it's a bit counter-intuitive that 1 way works and the other doesn't.

So my hunch is that the issue was caused by how TS goes about evaluating... when it's doing inference on a union of two nested conditional expressions, it may be evaluating twice, and merges the results together, meaning we end up with both string and Promise<string> that fit the bill.

(I'm asking around to get a better understanding of the mechanics - will let you know if it's caused by something else).

    1. This pull request is being automatically deployed with Vercel (learn more). To see the status of your deployment, click below or on the icon next to each commit.

      🔍 Inspect: https://vercel.com/tanstack/react-query/5MXumaLSNDFG8ovPuEH8yEo61p5K ✅ Preview: https://react-query-git-fork-artysidorenko-issue-3483-tanstack.vercel.app

    2. This pull request is automatically built and testable in CodeSandbox.

      To see build info of the built libraries, click here or the icon next to each commit SHA.

      Latest deployment of this branch, based on commit 913bda0f8c43fb79e448a84ad69e7dfbb8d47a9b:

      SandboxSource
      tannerlinsley/react-query: basicConfiguration
      tannerlinsley/react-query: basic-typescriptConfiguration
    3. Codecov Report

      Merging #3493 (913bda0) into beta (58a9fe9) will not change coverage. The diff coverage is n/a.

      @@           Coverage Diff           @@
      ##             beta    #3493   +/-   ##
      =======================================
        Coverage   96.94%   96.94%           
      =======================================
        Files          46       46           
        Lines        2390     2390           
        Branches      714      714           
      =======================================
        Hits         2317     2317           
        Misses         71       71           
        Partials        2        2           

      Continue to review full report at Codecov.

      Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 58a9fe9...913bda0. Read the comment docs.

    4. TkDodo approved these changes

      great work, as always. thank you 🙏

    5. TkDodo merged this5562cfa

    6. TkDodo closed this

    7. :tada: This PR is included in version 4.0.0-beta.3 :tada:

      The release is available on:

      Your semantic-release bot :package::rocket:

    8. tannerlinsley added labelsreleased on @beta

    9. src/reactjs/tests/useQueries.test.tsx line 786
      @@ -776,5 +776,11 @@
      776776 expectType<QueryObserverResult<string, unknown>>(result4[1])
      777777 expectType<QueryObserverResult<number, unknown>>(result4[2])
      778778
      779- // Array as const does not throw error
      779+ // handles when queryFn returns a Promise
      780780 const result5 = useQueries({
      781+ queries: [
      782+ {
      783+ queryKey: key1,
      784+ queryFn: () => Promise.resolve('string'),
      785+ onSuccess: (a: string) => {
      786+ expectType<string>(a)

      @artysidorenko Out of curiosity, what is the motivation for expectType and expectTypeNotAny here?

      Looking at their definitions, they are compile-time checks rather than runtime checks. In this case, the compile-time check is satisfied by (a: string) from the previous line in this test. In other words, it's testing the test code rather than the product code.

      Am I missing something? I'm not involved in this codebase, just noticed this and thought I'd ask.

      Hi @symbiont-matthew-pirocchi,

      Yeah, the expectType and expectTypeNotAny are not strictly necessary here, the way the types work at the moment. I think while I was writing the other tests I got into the pattern of test-asserting every type to avoid any surprises and help document a bit more what the types are at each step.

      Am still holding out hope that at some point we'll figure out a way to do the context-sensitive typing on the function parameter, or TS will release a new version that does this more effectively. Then we would replace that snippet with something like this, where the helpers are more important:

      onSuccess: (a) => {
        expectType<string>(a)
        expectTypeNotAny(a)
      },

      I suppose having these helpers in there already could be a good primer to keep the checks when that happens 🤞

      @artysidorenko I just stumbled upon eslint-plugin-expect-type, and the PR that adds support for two-slash annotations. Do you think that would be a good replace for type-level tests? https://twitter.com/joshuakgoldberg/status/1514382066795876352

      I'm sort of on the fence; the annotation-style looks cool, but reckon you'd still need the same volume of tests to check all the types (just replacing all those expectType calls with the two-slashes).

Sign in to join this conversation.