Skip to content

Unable to pass queryOptions to QueriesObserver (TypeScript error) #7446

Closed
@yannickcr

Description

@yannickcr

Describe the bug

When passing options created with the queryOptions helper to QueriesObserver we got the following TypeScript error as it is unable to assign UseQueryOptions to QueryObserverOptions:

Type 'UseQueryOptions<string, Error, string, string[]> & { initialData?: undefined; } & { queryKey: DataTag<string[], string>; }' is not assignable to type 'QueryObserverOptions<unknown, Error, unknown, unknown, QueryKey, never>'.
  Types of property 'refetchInterval' are incompatible.
    Type 'number | false | ((query: Query<string, Error, string, string[]>) => number | false | undefined) | undefined' is not assignable to type 'number | false | ((query: Query<unknown, Error, unknown, QueryKey>) => number | false | undefined) | undefined'.
      Type '(query: Query<string, Error, string, string[]>) => number | false | undefined' is not assignable to type 'number | false | ((query: Query<unknown, Error, unknown, QueryKey>) => number | false | undefined) | undefined'.
        Type '(query: Query<string, Error, string, string[]>) => number | false | undefined' is not assignable to type '(query: Query<unknown, Error, unknown, QueryKey>) => number | false | undefined'.
          Types of parameters 'query' and 'query' are incompatible.
            Type 'Query<unknown, Error, unknown, QueryKey>' is not assignable to type 'Query<string, Error, string, string[]>'.
              Types of property 'queryKey' are incompatible.
                The type 'QueryKey' is 'readonly' and cannot be assigned to the mutable type 'string[]'.

Your minimal, reproducible example

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgRQK4FMoE8DCAbYdAOxgBoUNsB5AIwGdMA3TctTQu2hqZqcgR0pYqYGMAhE6cAL5wAZlAgg4AcgACMAIaStAYwDWAeijpNumAFpBmLCoBQd3RLrxr2fIRJwAvHCLoAdwobD2IYAAoASgBuBycdOBh0FzZsHzg3YVFxSXCEOzgMoQBpdCwALjgAbRUQW3IVfTKVAF1SAqKbADEiSs06LCJdOCifAD5VOrhGTTwMFXbpSLjneDqu4CgXLiZMdP8g1OF6XahwzNCSciSUoRiOwsfCx1W4OoBldHiAEx2ePd8B2CUA4f145yElzI1RuMCOLXuTyRyJRqLR6IxhkMcAAenA6AALCCoPDfPwQeCYRRQFYJOoAFQJm1+J3+UH2gWBoNZ4IuBDC5Cq+UKmVKFWqtXqqiatjaHUyPT6AyGI0i40mWGms3miwR0SAA

Steps to reproduce

In the playground, see that QueryObserver accepts testQuery as options and that myFirstObserver type is correctly inferred to QueryObserver<string, Error, string, string, string[]>.

However QueriesObserver errors when passing testQuery as options. If we inline the object then we have no errors.

Also QueryObserverResult is not properly inferred (TData is always unknown) even when passing the options as an inline object (I can open another issue for this point if needed).

No issue at runtime, the code is working as expected.

Expected behavior

We should be able to pass the result of the queryOptions helper to QueriesObserver and have QueryObserverResult properly inferred.

Quoting the documentation on https://tanstack.com/query/latest/docs/reference/QueriesObserver#queriesobserver:

The options for the QueriesObserver are exactly the same as those of useQueries.

So, from what I understand, the queryOptions helper here should work too (and as you can see in the playground above, it actually works with QueryObserver, but not QueriesObserver).

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • macOS
  • Chrome 124

Tanstack Query adapter

react-query

TanStack Query version

v5.36.2

TypeScript version

v5.4.5

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions