From a87c2fe4a7ff8d6188a4d8733b4485415f6600ab Mon Sep 17 00:00:00 2001 From: Yannick Croissant Date: Wed, 17 Jul 2024 16:31:11 +0200 Subject: [PATCH] fix(query-core): widen QueriesObserver queries type (#7446) (#7492) * fix(query-core): widen QueriesObserver queries type (#7446) Widen the `QueriesObserver` queries type so we can assign `UseQueryOptions` to `QueryObserverOptions`. The result from the `queryOptions` helper can now be passed to `QueriesObserver`. * chore: prettier * fix(types): QueryKey needs to default to `any`, too --------- Co-authored-by: Yannick CROISSANT Co-authored-by: Dominik Dorfmeister --- packages/query-core/src/queriesObserver.ts | 2 +- .../src/__tests__/queryOptions.test-d.tsx | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/query-core/src/queriesObserver.ts b/packages/query-core/src/queriesObserver.ts index 8bfb33e954..2c58e9221c 100644 --- a/packages/query-core/src/queriesObserver.ts +++ b/packages/query-core/src/queriesObserver.ts @@ -45,7 +45,7 @@ export class QueriesObserver< constructor( client: QueryClient, - queries: Array, + queries: Array>, _options?: QueriesObserverOptions, ) { super() diff --git a/packages/react-query/src/__tests__/queryOptions.test-d.tsx b/packages/react-query/src/__tests__/queryOptions.test-d.tsx index dcb8d0ca47..242be3972c 100644 --- a/packages/react-query/src/__tests__/queryOptions.test-d.tsx +++ b/packages/react-query/src/__tests__/queryOptions.test-d.tsx @@ -1,9 +1,15 @@ import { describe, expect, expectTypeOf, it } from 'vitest' -import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core' +import { + QueriesObserver, + QueryClient, + dataTagSymbol, + skipToken, +} from '@tanstack/query-core' import { queryOptions } from '../queryOptions' import { useQuery } from '../useQuery' import { useQueries } from '../useQueries' import { useSuspenseQuery } from '../useSuspenseQuery' +import type { QueryObserverResult } from '@tanstack/query-core' describe('queryOptions', () => { it('should not allow excess properties', () => { @@ -169,4 +175,17 @@ describe('queryOptions', () => { const data = queryClient.getQueryData(options.queryKey) expectTypeOf(data).toEqualTypeOf() }) + + it('should return the proper type when passed to QueriesObserver', () => { + const options = queryOptions({ + queryKey: ['key'], + queryFn: () => Promise.resolve(5), + }) + + const queryClient = new QueryClient() + const queriesObserver = new QueriesObserver(queryClient, [options]) + expectTypeOf(queriesObserver).toEqualTypeOf< + QueriesObserver> + >() + }) })