Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions packages/query-core/src/__tests__/utils.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expectTypeOf, it } from 'vitest'
import { assertType, describe, expectTypeOf, it } from 'vitest'
import { QueryClient } from '../queryClient'
import type { QueryFilters } from '../utils'
import type { DataTag, QueryKey } from '../types'
Expand Down Expand Up @@ -35,11 +35,36 @@ describe('QueryFilters', () => {
}

const queryClient = new QueryClient()

const data = queryClient.getQueryData(a.queryKey!)
expectTypeOf(data).toEqualTypeOf<unknown>()

const error = queryClient.getQueryState(a.queryKey!)?.error
expectTypeOf(error).toEqualTypeOf<Error | null | undefined>()
})

it('should allow a partial query key to be passed', () => {
const filters: QueryFilters<readonly ['key', { a: number; b: string }]> = {
queryKey: ['key'],
}

expectTypeOf(filters.queryKey).toEqualTypeOf<
| undefined
| readonly []
| ['key']
| readonly [
'key',
{
a: number
b: string
},
]
>()
})

it('should error on invalid query keys', () => {
assertType<QueryFilters<readonly ['key', { a: number; b: string }]>>({
// @ts-expect-error cannot pass invalid query key
queryKey: ['invalid', { a: 1, b: '1' }],
})
})
})
10 changes: 10 additions & 0 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ export type Override<TTargetA, TTargetB> = {

export type NoInfer<T> = [T][T extends any ? 0 : never]

export type DropLast<T extends ReadonlyArray<unknown>> = T extends readonly [
...infer R,
unknown,
]
? R
: never

export type TuplePrefixes<T extends ReadonlyArray<unknown>> =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all those types become part of the public interface, I don’t think we want that so please just inline them in utils.ts

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good thought! I didn't even think of that. Updated.

T extends readonly [] ? readonly [] : TuplePrefixes<DropLast<T>> | T

export interface Register {
// defaultError: Error
// queryMeta: Record<string, unknown>
Expand Down
5 changes: 3 additions & 2 deletions packages/query-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
QueryOptions,
StaleTime,
StaleTimeFunction,
TuplePrefixes,
} from './types'
import type { Mutation } from './mutation'
import type { FetchOptions, Query } from './query'
Expand All @@ -32,7 +33,7 @@ export interface QueryFilters<TQueryKey extends QueryKey = QueryKey> {
/**
* Include queries matching this query key
*/
queryKey?: TQueryKey
queryKey?: TuplePrefixes<TQueryKey>
/**
* Include or exclude stale queries
*/
Expand Down Expand Up @@ -62,7 +63,7 @@ export interface MutationFilters<
/**
* Include mutations matching this mutation key
*/
mutationKey?: MutationKey
mutationKey?: TuplePrefixes<MutationKey>
/**
* Filter by mutation status
*/
Expand Down
Loading