Skip to content

Commit

Permalink
fix(types): enable more types for first QueryKey (#577)
Browse files Browse the repository at this point in the history
* fix(types): enable more types for first QueryKey

* fix(types): enable more types for first QueryKey

* test: update query key test case
  • Loading branch information
CreativeTechGuy committed Jun 14, 2020
1 parent 21b9b01 commit 87008db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 8 additions & 2 deletions types/index.d.ts
Expand Up @@ -339,17 +339,23 @@ export function useInfiniteQuery<
config?: InfiniteQueryOptions<TResult, TMoreVariable, TError>
): InfiniteQueryResult<TResult, TMoreVariable, TError>

export type DefinedQueryKeyPart =
| string
| object
| boolean
| number
| readonly QueryKeyPart[]

export type QueryKeyPart =
| string
| object
| boolean
| number
| null
| readonly QueryKeyPart[]
| null
| undefined

export type AnyQueryKey = readonly [string, ...QueryKeyPart[]] // this forces the key to be inferred as a tuple
export type AnyQueryKey = readonly [DefinedQueryKeyPart, ...QueryKeyPart[]] // this forces the key to be inferred as a tuple

export type AnyVariables = readonly [] | readonly [any, ...any[]] // this forces the variables to be inferred as a tuple

Expand Down
10 changes: 7 additions & 3 deletions types/test.ts
Expand Up @@ -33,9 +33,13 @@ function queryWithVariables() {
queryVariables.refetch({ force: true }) // $ExpectType Promise<boolean>
}

function invalidSimpleQuery() {
// first element in the key must be a string
useQuery([10, 'a'], async (id, key) => id) // $ExpectError
function queryKeyArrayOrder() {
// first element in the key must be not null/undefined
useQuery([null, 'a'], async (id, key) => id) // $ExpectError

useQuery([10, 'a'], async (id, key) => id)
useQuery([false, 'a'], async (id, key) => id)
useQuery([{ complex: { obj: "yes" } }, 'a'], async (id, key) => id)
}

function conditionalQuery(condition: boolean) {
Expand Down

0 comments on commit 87008db

Please sign in to comment.