query icon indicating copy to clipboard operation
query copied to clipboard

fix(query-core) Fix queries argument's type definition in queriesObserver

Open yanlin-duan opened this issue 7 months ago • 4 comments

queries argument is not typed consistently in many of the methods in queriesObserver class. The constructor typed it to be Array<QueryObserverOptions<any, any, any, any, any>> but in few other places it's Array<QueryObserverOptions> which means the default will kick in, making it Array<QueryObserverOptions<unknown, DefaultError, unknown, unknown, QueryKey>.

This caused issues when I try to interact with queryObserver:

const queryOptions: Array<QueryObserverOptions<Data, Error, SelectedData, Data, QueryKey>> = { } // some query options

// defaultedOptions typed as DefaultedQueryObserverOptions<unknown, Error, unknown, unknown, QueryKey>[]
const defaultedOptions = queryOptions.map(queryOption => {
        const defaultedOption = queryClient.defaultQueryOptions(queryOption)
        defaultedOption._optimisticResults = isRestoring
          ? 'isRestoring'
          : 'optimistic'
        return defaultedOption
})

// OK
const queriesObserver = new QueriesObserver<Array<QueryObserverResult<SelectedData, Error>>>(
      queryClient,
      defaultedOptions
)

// ERROR
// Argument of type 'DefaultedQueryObserverOptions<Data, Error, SelectedData, Data, QueryKey>[]' is not assignable to parameter of type 'QueryObserverOptions<unknown, Error, unknown, unknown, QueryKey, never>[]'.
//   Type 'DefaultedQueryObserverOptions<Data, Error, SelectedData, Data, QueryKey>' is not assignable to type 'QueryObserverOptions<unknown, Error, unknown, unknown, QueryKey, never>'.
//     Types of property 'enabled' are incompatible.
//       Type 'Enabled<Data, Error, Data, QueryKey> | undefined' is not assignable to type 'Enabled<unknown, Error, unknown, QueryKey> | undefined'.
//         Type '(query: Query<Data, Error, Data, QueryKey>) => boolean' is not assignable to type 'Enabled<unknown, Error, unknown, QueryKey> | undefined'.
//           Type '(query: Query<Data, Error, Data, QueryKey>) => boolean' is not assignable to type '(query: Query<unknown, Error, unknown, QueryKey>) => boolean'.
//             Types of parameters 'query' and 'query' are incompatible.
//               Type 'Query<unknown, Error, unknown, QueryKey>' is not assignable to type 'Query<Data, Error, Data, QueryKey>'.
//                 The types of 'options.queryFn' are incompatible between these types.
//                   Type 'unique symbol | QueryFunction<unknown, QueryKey, never> | undefined' is not assignable to type 'unique symbol | QueryFunction<Data, QueryKey, never> | undefined'.
//                     Type 'QueryFunction<unknown, QueryKey, never>' is not assignable to type 'unique symbol | QueryFunction<Data, QueryKey, never> | undefined'.
//                       Type 'QueryFunction<unknown, QueryKey, never>' is not assignable to type 'QueryFunction<Data, QueryKey, never>'.
//                         Type 'unknown' is not assignable to type 'Data'.
//                           'Data' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.ts(2345)
queriesObserver.setQueries(defaultedOptions)

In the fix, I used the same type that the constructor takes in for queryOptions in setQueries and a few other places.

yanlin-duan avatar Jul 17 '25 17:07 yanlin-duan

View your CI Pipeline Execution ↗ for commit 3ee21cff82b676ebd6375a468ace69b2ee97179d

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 2m 50s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1m 21s View ↗

☁️ Nx Cloud last updated this comment at 2025-07-30 09:58:21 UTC

nx-cloud[bot] avatar Jul 30 '25 09:07 nx-cloud[bot]

More templates

@tanstack/angular-query-devtools-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@9449
@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9449
@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9449
@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9449
@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9449
@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@9449
@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@9449
@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9449
@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9449
@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@9449
@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@9449
@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9449
@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9449
@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@9449
@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9449
@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9449
@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@9449
@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9449
@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9449
@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@9449
@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9449

commit: 3ee21cf

pkg-pr-new[bot] avatar Jul 30 '25 09:07 pkg-pr-new[bot]

why don’t we have the problem you’re describing here:

https://github.com/TanStack/query/blob/07cbfe1b364fea5833feb01273a5f661fe2d8ff6/packages/react-query/src/useQueries.ts#L282-L283

?

TkDodo avatar Jul 30 '25 09:07 TkDodo

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 59.57%. Comparing base (cc45977) to head (3ee21cf).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #9449       +/-   ##
===========================================
+ Coverage   45.30%   59.57%   +14.26%     
===========================================
  Files         208      137       -71     
  Lines        8283     5521     -2762     
  Branches     1872     1485      -387     
===========================================
- Hits         3753     3289      -464     
+ Misses       4085     1930     -2155     
+ Partials      445      302      -143     
Components Coverage Δ
@tanstack/angular-query-devtools-experimental ∅ <ø> (∅)
@tanstack/angular-query-experimental 85.00% <ø> (ø)
@tanstack/eslint-plugin-query ∅ <ø> (∅)
@tanstack/query-async-storage-persister 43.85% <ø> (ø)
@tanstack/query-broadcast-client-experimental 24.39% <ø> (ø)
@tanstack/query-codemods ∅ <ø> (∅)
@tanstack/query-core 97.69% <ø> (ø)
@tanstack/query-devtools 3.55% <ø> (ø)
@tanstack/query-persist-client-core 79.47% <ø> (ø)
@tanstack/query-sync-storage-persister 84.61% <ø> (ø)
@tanstack/query-test-utils ∅ <ø> (∅)
@tanstack/react-query 95.95% <ø> (ø)
@tanstack/react-query-devtools 10.00% <ø> (ø)
@tanstack/react-query-next-experimental ∅ <ø> (∅)
@tanstack/react-query-persist-client 100.00% <ø> (ø)
@tanstack/solid-query 78.13% <ø> (ø)
@tanstack/solid-query-devtools ∅ <ø> (∅)
@tanstack/solid-query-persist-client 100.00% <ø> (ø)
@tanstack/svelte-query 87.58% <ø> (ø)
@tanstack/svelte-query-devtools ∅ <ø> (∅)
@tanstack/svelte-query-persist-client 100.00% <ø> (ø)
@tanstack/vue-query 71.10% <ø> (ø)
@tanstack/vue-query-devtools ∅ <ø> (∅)
:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jul 30 '25 09:07 codecov[bot]