Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): improve v2 types #655

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
75 changes: 41 additions & 34 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export interface BaseSharedOptions {
suspense: boolean
}

export interface BaseQueryOptions<TError = Error> {
export interface BaseQueryOptions<TResult = unknown, TError = Error> {
/**
* Set this to `false` 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.
Expand All @@ -237,18 +237,16 @@ export interface BaseQueryOptions<TError = Error> {
refetchIntervalInBackground?: boolean
refetchOnWindowFocus?: boolean
refetchOnMount?: boolean
onSuccess?: (data: any) => void
onSuccess?: (data: TResult) => void
onError?: (err: TError) => void
onSettled?: (data: any, error: TError | null) => void
onSettled?: (data: TResult | undefined, error: TError | null) => void
isDataEqual?: (oldData: unknown, newData: unknown) => boolean
useErrorBoundary?: boolean
}

export interface QueryOptions<TResult, TError = Error>
extends BaseQueryOptions<TError> {
extends BaseQueryOptions<TResult, TError> {
suspense?: boolean
onSuccess?: (data: TResult) => void
onSettled?: (data: TResult | undefined, error: TError | null) => void
initialData?: TResult | (() => TResult | undefined)
initialStale?: boolean | (() => boolean | undefined)
}
Expand Down Expand Up @@ -387,37 +385,57 @@ export interface InfiniteQueryResult<TResult, TMoreVariable, TError = Error>
) => Promise<TResult[]> | undefined
}

export function useMutation<TResults, TVariables = undefined, TError = Error>(
mutationFn: MutationFunction<TResults, TVariables, TError>,
mutationOptions?: MutationOptions<TResults, TVariables, TError>
export function useMutation<
TResult,
TVariables = undefined,
TError = Error,
TSnapshot = unknown
>(
mutationFn: MutationFunction<TResult, TVariables, TError, TSnapshot>,
mutationOptions?: MutationOptions<TResult, TVariables, TError, TSnapshot>
): [
MutateFunction<TResults, TVariables, TError>,
MutationResult<TResults, TError>
MutateFunction<TResult, TVariables, TError>,
MutationResult<TResult, TError>
]

export type MutationFunction<TResults, TVariables, TError = Error> = (
export type MutationFunction<
TResult,
TVariables,
TError = Error,
TSnapshot = unknown
> = (
variables: TVariables,
mutateOptions?: MutateOptions<TResults, TVariables, TError>
) => Promise<TResults>
mutateOptions?: MutateOptions<TResult, TVariables, TError, TSnapshot>
) => Promise<TResult>

export interface MutateOptions<TResult, TVariables, TError = Error> {
export interface MutateOptions<
TResult,
TVariables,
TError = Error,
TSnapshot = unknown
> {
onSuccess?: (data: TResult, variables: TVariables) => Promise<void> | void
onError?: (
error: TError,
snapshotValue: unknown,
onMutateValue: (variable: TVariables) => Promise<unknown> | unknown
variables: TVariables,
snapshotValue: TSnapshot
) => Promise<void> | void
onSettled?: (
data: undefined | TResult,
error: TError | null,
snapshotValue?: unknown
variables: TVariables,
snapshotValue?: TSnapshot
) => Promise<void> | void
throwOnError?: boolean
}

export interface MutationOptions<TResult, TVariables, TError = Error>
extends MutateOptions<TResult, TVariables, TError> {
onMutate?: (variables: TVariables) => Promise<unknown> | unknown
export interface MutationOptions<
TResult,
TVariables,
TError = Error,
TSnapshot = unknown
> extends MutateOptions<TResult, TVariables, TError, TSnapshot> {
onMutate?: (variables: TVariables) => Promise<TSnapshot> | TSnapshot
useErrorBoundary?: boolean
}

Expand Down Expand Up @@ -605,7 +623,7 @@ export interface QueryCache {
): void
isFetching: number
subscribe(callback: (queryCache: QueryCache) => void): () => void
clear(): void,
clear(): void
resetErrorBoundaries: () => void
}

Expand Down Expand Up @@ -655,18 +673,7 @@ export interface ReactQueryProviderConfig<TError = Error> {
) => [string, QueryKeyPart[]] | []
}
shared?: BaseSharedOptions
mutations?: {
throwOnError?: boolean
useErrorBoundary?: boolean
onMutate?: (variables: unknown) => Promise<unknown> | unknown
onSuccess?: (data: unknown, variables?: unknown) => void
onError?: (err: TError, snapshotValue?: unknown) => void
onSettled?: (
data: unknown | undefined,
error: TError | null,
snapshotValue?: unknown
) => void
}
mutations?: MutationOptions<unknown, unknown, TError>
}

export type ConsoleFunction = (...args: any[]) => void
Expand Down
9 changes: 7 additions & 2 deletions types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,14 @@ function globalConfig() {
useErrorBoundary: true,
onMutate: (variables: unknown) => Promise.resolve(),
onSuccess: (data: unknown, variables: unknown) => undefined,
onError: (err: Error, snapshotValue: unknown) => undefined,
onSettled: (data: unknown, error: Error | null, snapshotValue?: any) =>
onError: (err: Error, variables: unknown, snapshotValue: unknown) =>
undefined,
onSettled: (
data: unknown,
error: Error | null,
variables: unknown,
snapshotValue?: unknown
) => undefined,
},
}
}
Expand Down