diff --git a/types/index.d.ts b/types/index.d.ts index 803176f9a7..474d663827 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -11,7 +11,8 @@ import * as _ from 'ts-toolbelt' export function useQuery< TResult, TKey extends AnyQueryKey, - TVariables extends AnyVariables = [] + TVariables extends AnyVariables = [], + TError = Error >({ queryKey, variables, @@ -26,13 +27,14 @@ export function useQuery< | (() => TKey | false | null | undefined) variables?: TVariables queryFn: QueryFunctionWithVariables - config?: QueryOptions -}): QueryResult + config?: QueryOptions +}): QueryResult export function useQuery< TResult, TSingleKey extends string, - TVariables extends AnyVariables = [] + TVariables extends AnyVariables = [], + TError = Error >({ queryKey, variables, @@ -47,10 +49,10 @@ export function useQuery< | (() => TSingleKey | false | null | undefined) variables?: TVariables queryFn: QueryFunctionWithVariables - config?: QueryOptions -}): QueryResult + config?: QueryOptions +}): QueryResult -export function useQuery( +export function useQuery( queryKey: | TKey | false @@ -58,10 +60,10 @@ export function useQuery( | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, - config?: QueryOptions -): QueryResult + config?: QueryOptions +): QueryResult -export function useQuery( +export function useQuery( queryKey: | TSingleKey | false @@ -69,13 +71,14 @@ export function useQuery( | undefined | (() => TSingleKey | false | null | undefined), queryFn: QueryFunction, - config?: QueryOptions -): QueryResult + config?: QueryOptions +): QueryResult export function useQuery< TResult, TKey extends AnyQueryKey, - TVariables extends AnyVariables + TVariables extends AnyVariables, + TError = Error >( queryKey: | TKey @@ -85,13 +88,14 @@ export function useQuery< | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, - config?: QueryOptions -): QueryResult + config?: QueryOptions +): QueryResult export function useQuery< TResult, TKey extends string, - TVariables extends AnyVariables + TVariables extends AnyVariables, + TError = Error >( queryKey: | TKey @@ -101,14 +105,15 @@ export function useQuery< | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, - config?: QueryOptions -): QueryResult + config?: QueryOptions +): QueryResult // usePaginatedQuery export function usePaginatedQuery< TResult, TKey extends AnyQueryKey, - TVariables extends AnyVariables = [] + TVariables extends AnyVariables = [], + TError = Error >({ queryKey, variables, @@ -123,13 +128,14 @@ export function usePaginatedQuery< | (() => TKey | false | null | undefined) variables?: TVariables queryFn: QueryFunctionWithVariables - config?: QueryOptions -}): PaginatedQueryResult + config?: QueryOptions +}): PaginatedQueryResult export function usePaginatedQuery< TResult, TSingleKey extends string, - TVariables extends AnyVariables = [] + TVariables extends AnyVariables = [], +TError = Error >({ queryKey, variables, @@ -144,10 +150,10 @@ export function usePaginatedQuery< | (() => TSingleKey | false | null | undefined) variables?: TVariables queryFn: QueryFunctionWithVariables - config?: QueryOptions -}): PaginatedQueryResult + config?: QueryOptions +}): PaginatedQueryResult -export function usePaginatedQuery( +export function usePaginatedQuery( queryKey: | TKey | false @@ -155,10 +161,10 @@ export function usePaginatedQuery( | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, - config?: QueryOptions -): PaginatedQueryResult + config?: QueryOptions +): PaginatedQueryResult -export function usePaginatedQuery( +export function usePaginatedQuery( queryKey: | TKey | false @@ -166,13 +172,14 @@ export function usePaginatedQuery( | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, - config?: QueryOptions -): PaginatedQueryResult + config?: QueryOptions +): PaginatedQueryResult export function usePaginatedQuery< TResult, TKey extends AnyQueryKey, - TVariables extends AnyVariables + TVariables extends AnyVariables, +TError = Error >( queryKey: | TKey @@ -182,13 +189,14 @@ export function usePaginatedQuery< | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, - config?: QueryOptions -): PaginatedQueryResult + config?: QueryOptions +): PaginatedQueryResult export function usePaginatedQuery< TResult, TKey extends string, - TVariables extends AnyVariables + TVariables extends AnyVariables, + TError = Error >( queryKey: | TKey @@ -198,15 +206,16 @@ export function usePaginatedQuery< | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, - config?: QueryOptions -): PaginatedQueryResult + config?: QueryOptions +): PaginatedQueryResult // useInfiniteQuery export function useInfiniteQuery< TResult, TKey extends AnyQueryKey, TMoreVariable, - TVariables extends AnyVariables = [] + TVariables extends AnyVariables = [], + TError = Error >({ queryKey, variables, @@ -226,14 +235,15 @@ export function useInfiniteQuery< TVariables, TMoreVariable > - config?: InfiniteQueryOptions -}): InfiniteQueryResult + config?: InfiniteQueryOptions +}): InfiniteQueryResult export function useInfiniteQuery< TResult, TSingleKey extends string, TMoreVariable, - TVariables extends AnyVariables = [] + TVariables extends AnyVariables = [], + TError = Error >({ queryKey, variables, @@ -253,13 +263,14 @@ export function useInfiniteQuery< TVariables, TMoreVariable > - config?: InfiniteQueryOptions -}): InfiniteQueryResult + config?: InfiniteQueryOptions +}): InfiniteQueryResult export function useInfiniteQuery< TResult, TKey extends AnyQueryKey, - TMoreVariable + TMoreVariable, + TError = Error >( queryKey: | TKey @@ -268,10 +279,10 @@ export function useInfiniteQuery< | undefined | (() => TKey | false | null | undefined), queryFn: InfiniteQueryFunction, - config?: InfiniteQueryOptions -): InfiniteQueryResult + config?: InfiniteQueryOptions +): InfiniteQueryResult -export function useInfiniteQuery( +export function useInfiniteQuery( queryKey: | TKey | false @@ -279,14 +290,15 @@ export function useInfiniteQuery( | undefined | (() => TKey | false | null | undefined), queryFn: InfiniteQueryFunction, - config?: InfiniteQueryOptions -): InfiniteQueryResult + config?: InfiniteQueryOptions +): InfiniteQueryResult export function useInfiniteQuery< TResult, TKey extends AnyQueryKey, TVariables extends AnyVariables, - TMoreVariable + TMoreVariable, + TError = Error >( queryKey: | TKey @@ -301,14 +313,15 @@ export function useInfiniteQuery< TVariables, TMoreVariable >, - config?: InfiniteQueryOptions -): InfiniteQueryResult + config?: InfiniteQueryOptions +): InfiniteQueryResult export function useInfiniteQuery< TResult, TKey extends string, TVariables extends AnyVariables, - TMoreVariable + TMoreVariable, + TError = Error >( queryKey: | TKey @@ -323,8 +336,8 @@ export function useInfiniteQuery< TVariables, TMoreVariable >, - config?: InfiniteQueryOptions -): InfiniteQueryResult + config?: InfiniteQueryOptions +): InfiniteQueryResult export type QueryKeyPart = | string @@ -369,7 +382,7 @@ export type InfiniteQueryFunctionWithVariables< | _.List.Concat ) => Promise -export interface BaseQueryOptions { +export interface BaseQueryOptions { /** * Set this to `true` to disable automatic refetching when the query mounts or changes query keys. * To refetch the query, use the `refetch` method returned from the `useQuery` instance. @@ -381,7 +394,7 @@ export interface BaseQueryOptions { * If set to an integer number, e.g. 3, failed queries will retry until the failed query count meets that number. * If set to a function `(failureCount, error) => boolean` failed queries will retry until the function returns false. */ - retry?: boolean | number | ((failureCount: number, error: Error) => boolean) + retry?: boolean | number | ((failureCount: number, error: TError) => boolean) retryDelay?: (retryAttempt: number) => number staleTime?: number cacheTime?: number @@ -389,33 +402,33 @@ export interface BaseQueryOptions { refetchIntervalInBackground?: boolean refetchOnWindowFocus?: boolean refetchOnMount?: boolean - onError?: (err: Error) => void + onError?: (err: TError) => void suspense?: boolean isDataEqual?: (oldData: unknown, newData: unknown) => boolean } -export interface QueryOptions extends BaseQueryOptions { +export interface QueryOptions extends BaseQueryOptions { onSuccess?: (data: TResult) => void - onSettled?: (data: TResult | undefined, error: Error | null) => void + onSettled?: (data: TResult | undefined, error: TError | null) => void initialData?: TResult | (() => TResult | undefined) } -export interface PrefetchQueryOptions extends QueryOptions { +export interface PrefetchQueryOptions extends QueryOptions { force?: boolean throwOnError?: boolean } -export interface InfiniteQueryOptions - extends QueryOptions { +export interface InfiniteQueryOptions + extends QueryOptions { getFetchMore: ( lastPage: TResult, allPages: TResult[] ) => TMoreVariable | false } -export interface QueryResultBase { +export interface QueryResultBase { status: 'loading' | 'error' | 'success' - error: null | Error + error: null | TError isFetching: boolean isStale: boolean failureCount: number @@ -428,16 +441,16 @@ export interface QueryResultBase { }) => Promise } -export interface QueryLoadingResult extends QueryResultBase { +export interface QueryLoadingResult extends QueryResultBase { status: 'loading' data: TResult | undefined // even when error, data can have stale data - error: null | Error // it still can be error + error: TError | null // it still can be error } -export interface QueryErrorResult extends QueryResultBase { +export interface QueryErrorResult extends QueryResultBase { status: 'error' data: TResult | undefined // even when error, data can have stale data - error: Error + error: TError } export interface QuerySuccessResult extends QueryResultBase { @@ -446,25 +459,25 @@ export interface QuerySuccessResult extends QueryResultBase { error: null } -export type QueryResult = - | QueryLoadingResult - | QueryErrorResult +export type QueryResult = + | QueryLoadingResult + | QueryErrorResult | QuerySuccessResult -export interface PaginatedQueryLoadingResult - extends QueryResultBase { +export interface PaginatedQueryLoadingResult + extends QueryResultBase { status: 'loading' resolvedData: undefined | TResult // even when error, data can have stale data latestData: undefined | TResult // even when error, data can have stale data - error: null | Error // it still can be error + error: null | TError // it still can be error } -export interface PaginatedQueryErrorResult - extends QueryResultBase { +export interface PaginatedQueryErrorResult + extends QueryResultBase { status: 'error' resolvedData: undefined | TResult // even when error, data can have stale data latestData: undefined | TResult // even when error, data can have stale data - error: Error + error: TError } export interface PaginatedQuerySuccessResult @@ -475,13 +488,13 @@ export interface PaginatedQuerySuccessResult error: null } -export type PaginatedQueryResult = - | PaginatedQueryLoadingResult - | PaginatedQueryErrorResult +export type PaginatedQueryResult = + | PaginatedQueryLoadingResult + | PaginatedQueryErrorResult | PaginatedQuerySuccessResult -export interface InfiniteQueryResult - extends QueryResultBase { +export interface InfiniteQueryResult + extends QueryResultBase { data: TResult[] isFetchingMore: boolean canFetchMore: boolean | undefined @@ -490,74 +503,74 @@ export interface InfiniteQueryResult ) => Promise | undefined } -export function useMutation( +export function useMutation( mutationFn: MutationFunction, - mutationOptions?: MutationOptions -): [MutateFunction, MutationResult] + mutationOptions?: MutationOptions +): [MutateFunction, MutationResult] export type MutationFunction = ( variables: TVariables ) => Promise -export interface MutateOptions { +export interface MutateOptions { onSuccess?: (data: TResult, variables: TVariables) => Promise | void onError?: ( - error: Error, + error: TError, variables: TVariables, snapshotValue: unknown ) => Promise | void onSettled?: ( data: undefined | TResult, - error: Error | null, + error: TError | null, variables: TVariables, snapshotValue?: unknown ) => Promise | void throwOnError?: boolean } -export interface MutationOptions - extends MutateOptions { +export interface MutationOptions + extends MutateOptions { onMutate?: (variables: TVariables) => Promise | unknown useErrorBoundary?: boolean } -export type MutateFunction = undefined extends TVariables +export type MutateFunction = undefined extends TVariables ? ( variables?: TVariables, - options?: MutateOptions + options?: MutateOptions ) => Promise : ( variables: TVariables, - options?: MutateOptions + options?: MutateOptions ) => Promise -export interface MutationResultBase { +export interface MutationResultBase { status: 'idle' | 'loading' | 'error' | 'success' data: undefined | TResult - error: undefined | null | Error + error: undefined | null | TError promise: Promise reset: () => void } -export interface IdleMutationResult - extends MutationResultBase { +export interface IdleMutationResult + extends MutationResultBase { status: 'idle' data: undefined error: null } -export interface LoadingMutationResult - extends MutationResultBase { +export interface LoadingMutationResult + extends MutationResultBase { status: 'loading' data: undefined error: undefined } -export interface ErrorMutationResult - extends MutationResultBase { +export interface ErrorMutationResult + extends MutationResultBase { status: 'error' data: undefined - error: Error + error: TError } export interface SuccessMutationResult @@ -567,10 +580,10 @@ export interface SuccessMutationResult error: undefined } -export type MutationResult = - | IdleMutationResult - | LoadingMutationResult - | ErrorMutationResult +export type MutationResult = + | IdleMutationResult + | LoadingMutationResult + | ErrorMutationResult | SuccessMutationResult export interface CachedQueryState { @@ -584,11 +597,11 @@ export interface CachedQueryState { updatedAt: number } -export interface CachedQuery { +export interface CachedQuery { queryKey: AnyQueryKey queryVariables: AnyVariables queryFn: (...args: any[]) => unknown - config: QueryOptions + config: QueryOptions state: CachedQueryState setData( dataOrUpdater: unknown | ((oldData: unknown | undefined) => unknown) @@ -597,7 +610,7 @@ export interface CachedQuery { } export interface QueryCache { - prefetchQuery( + prefetchQuery( queryKey: | TKey | false @@ -605,10 +618,10 @@ export interface QueryCache { | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, - config?: PrefetchQueryOptions + config?: PrefetchQueryOptions ): Promise - prefetchQuery( + prefetchQuery( queryKey: | TKey | false @@ -616,13 +629,14 @@ export interface QueryCache { | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, - config?: PrefetchQueryOptions + config?: PrefetchQueryOptions ): Promise prefetchQuery< TResult, TKey extends AnyQueryKey, - TVariables extends AnyVariables + TVariables extends AnyVariables, + TError = Error >( queryKey: | TKey @@ -632,10 +646,10 @@ export interface QueryCache { | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, - config?: PrefetchQueryOptions + config?: PrefetchQueryOptions ): Promise - prefetchQuery( + prefetchQuery( queryKey: | TKey | false @@ -644,13 +658,14 @@ export interface QueryCache { | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, - config?: PrefetchQueryOptions + config?: PrefetchQueryOptions ): Promise prefetchQuery< TResult, TKey extends AnyQueryKey, - TVariables extends AnyVariables = [] + TVariables extends AnyVariables = [], + TError = Error >({ queryKey, variables, @@ -665,7 +680,7 @@ export interface QueryCache { | (() => TKey | false | null | undefined) variables?: TVariables queryFn: QueryFunctionWithVariables - config?: PrefetchQueryOptions + config?: PrefetchQueryOptions }): Promise getQueryData(key: AnyQueryKey | string): T | undefined @@ -732,7 +747,7 @@ export const ReactQueryConfigProvider: React.ComponentType<{ config?: ReactQueryProviderConfig }> -export interface ReactQueryProviderConfig extends BaseQueryOptions { +export interface ReactQueryProviderConfig extends BaseQueryOptions { /** Defaults to the value of `suspense` if not defined otherwise */ useErrorBoundary?: boolean throwOnError?: boolean @@ -748,10 +763,10 @@ export interface ReactQueryProviderConfig extends BaseQueryOptions { onMutate?: (variables: unknown) => Promise | unknown onSuccess?: (data: unknown, variables?: unknown) => void - onError?: (err: Error, snapshotValue?: unknown) => void + onError?: (err: TError, snapshotValue?: unknown) => void onSettled?: ( data: unknown | undefined, - error: Error | null, + error: TError | null, snapshotValue?: unknown ) => void isDataEqual?: (oldData: unknown, newData: unknown) => boolean