From 422c879cffa90367301265732016245a8fe39421 Mon Sep 17 00:00:00 2001 From: Nick Lucas Date: Wed, 27 Nov 2024 08:03:51 +0000 Subject: [PATCH] fix(types): Add types for QueryFilters which flow down to Query<> and QueryKey/DataTag types (#8332) * Add types for QueryFilters which flow down to Query<> and QueryKey/DataTag types * ci: apply automated fixes * Add a type test * ci: apply automated fixes * Fix linting --------- Co-authored-by: Dominik Dorfmeister Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- .../query-core/src/__tests__/utils.test-d.tsx | 45 +++++++++++++++++++ packages/query-core/src/types.ts | 14 +++++- packages/query-core/src/utils.ts | 14 ++++-- 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 packages/query-core/src/__tests__/utils.test-d.tsx diff --git a/packages/query-core/src/__tests__/utils.test-d.tsx b/packages/query-core/src/__tests__/utils.test-d.tsx new file mode 100644 index 0000000000..f2ac65bfcc --- /dev/null +++ b/packages/query-core/src/__tests__/utils.test-d.tsx @@ -0,0 +1,45 @@ +import { describe, expectTypeOf, it } from 'vitest' +import { QueryClient } from '../queryClient' +import type { QueryFilters } from '../utils' +import type { DataTag } from '../types' + +describe('QueryFilters', () => { + it('should be typed if generics are passed', () => { + type TData = { a: number; b: string } + type TError = { message: string } + + const a: QueryFilters = { + predicate(query) { + expectTypeOf(query.setData({ a: 1, b: '1' })).toEqualTypeOf() + return true + }, + queryKey: ['key'] as DataTag, + } + + const queryClient = new QueryClient() + + const data = queryClient.getQueryData(a.queryKey!) + expectTypeOf(data).toEqualTypeOf() + + const error = queryClient.getQueryState(a.queryKey!)?.error + expectTypeOf(error).toEqualTypeOf() // maybe one day this can return TError + }) + + it('should be loose typed if generics are defaults', () => { + const a: QueryFilters = { + predicate(query) { + expectTypeOf(query.setData({ a: 1, b: '1' })).toEqualTypeOf() + return true + }, + queryKey: ['key'], + } + + const queryClient = new QueryClient() + + const data = queryClient.getQueryData(a.queryKey!) + expectTypeOf(data).toEqualTypeOf() + + const error = queryClient.getQueryState(a.queryKey!)?.error + expectTypeOf(error).toEqualTypeOf() + }) +}) diff --git a/packages/query-core/src/types.ts b/packages/query-core/src/types.ts index b13e52d16c..bbf413d1e9 100644 --- a/packages/query-core/src/types.ts +++ b/packages/query-core/src/types.ts @@ -534,11 +534,21 @@ export interface RefetchOptions extends ResultOptions { cancelRefetch?: boolean } -export interface InvalidateQueryFilters extends QueryFilters { +export interface InvalidateQueryFilters< + TQueryFnData = unknown, + TError = Error, + TData = TQueryFnData, + TQueryKey extends QueryKey = QueryKey, +> extends QueryFilters { refetchType?: QueryTypeFilter | 'none' } -export interface RefetchQueryFilters extends QueryFilters {} +export interface RefetchQueryFilters< + TQueryFnData = unknown, + TError = Error, + TData = TQueryFnData, + TQueryKey extends QueryKey = QueryKey, +> extends QueryFilters {} export interface InvalidateOptions extends RefetchOptions {} export interface ResetOptions extends RefetchOptions {} diff --git a/packages/query-core/src/utils.ts b/packages/query-core/src/utils.ts index b4ceed2fe4..07c134e4ae 100644 --- a/packages/query-core/src/utils.ts +++ b/packages/query-core/src/utils.ts @@ -1,4 +1,5 @@ import type { + DataTag, DefaultError, Enabled, FetchStatus, @@ -14,7 +15,12 @@ import type { FetchOptions, Query } from './query' // TYPES -export interface QueryFilters { +export interface QueryFilters< + TQueryFnData = unknown, + TError = Error, + TData = TQueryFnData, + TQueryKey extends QueryKey = QueryKey, +> { /** * Filter to active queries, inactive queries or all queries */ @@ -26,11 +32,13 @@ export interface QueryFilters { /** * Include queries matching this predicate function */ - predicate?: (query: Query) => boolean + predicate?: (query: Query) => boolean /** * Include queries matching this query key */ - queryKey?: QueryKey + queryKey?: unknown extends TQueryFnData + ? QueryKey + : QueryKey & DataTag /** * Include or exclude stale queries */