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(react-query): types for useSuspenseInfiniteQuery #5766

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
25 changes: 25 additions & 0 deletions packages/react-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
DefinedQueryObserverResult,
InfiniteQueryObserverOptions,
InfiniteQueryObserverResult,
InfiniteQueryObserverSuccessResult,
MutateFunction,
MutationObserverOptions,
MutationObserverResult,
Expand Down Expand Up @@ -66,6 +67,25 @@ export interface UseInfiniteQueryOptions<
'queryKey'
> {}

export interface UseSuspenseInfiniteQueryOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
> extends Omit<
UseInfiniteQueryOptions<
TQueryFnData,
TError,
TData,
TQueryData,
TQueryKey,
TPageParam
>,
'enabled' | 'suspense' | 'throwOnError' | 'placeholderData'
> {}
Comment on lines +70 to +87
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added UseSuspenseInfiniteQueryOptions, cause of same reason with why we need UseSuspenseQueryOptions


export type UseBaseQueryResult<
TData = unknown,
TError = DefaultError,
Expand Down Expand Up @@ -96,6 +116,11 @@ export type DefinedUseInfiniteQueryResult<
TError = DefaultError,
> = DefinedInfiniteQueryObserverResult<TData, TError>

export type UseSuspenseInfiniteQueryResult<
TData = unknown,
TError = DefaultError,
> = Omit<InfiniteQueryObserverSuccessResult<TData, TError>, 'isPlaceholderData'>
Comment on lines +119 to +122
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought If we don't have enabled option and have default suspense option, we don't have to use DefinedUseInfiniteQueryResult containing unnecessary InfiniteQueryObserverRefetchErrorResult.

type DefinedInfiniteQueryObserverResult<TData = unknown, TError = DefaultError> = InfiniteQueryObserverRefetchErrorResult<TData, TError> | InfiniteQueryObserverSuccessResult<TData, TError>;

We just need only InfiniteQueryObserverSuccessResult for useSuspenseInfiniteQuery.
So we could remove isSuccess: false, error, isError: true etc. to do type-narrow

interface InfiniteQueryObserverRefetchErrorResult<TData = unknown, TError = DefaultError> extends InfiniteQueryObserverBaseResult<TData, TError> {
    data: TData;
    error: TError;
    isError: true;
    isPending: false;
    isLoadingError: false;
    isRefetchError: true;
    isSuccess: false;
    status: 'error';
}
interface InfiniteQueryObserverSuccessResult<TData = unknown, TError = DefaultError> extends InfiniteQueryObserverBaseResult<TData, TError> {
    data: TData;
    error: null;
    isError: false;
    isPending: false;
    isLoadingError: false;
    isRefetchError: false;
    isSuccess: true;
    status: 'success';
}

This change is needed by same reason with #5755 (comment)


export interface UseMutationOptions<
TData = unknown,
TError = DefaultError,
Expand Down
32 changes: 17 additions & 15 deletions packages/react-query/src/useSuspenseInfiniteQuery.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
'use client'
import { InfiniteQueryObserver } from '@tanstack/query-core'
import { useBaseQuery } from './useBaseQuery'
import type { QueryObserver } from '@tanstack/query-core'
import type {
InfiniteQueryObserverSuccessResult,
QueryObserver,
} from '@tanstack/query-core'
import type {
DefaultError,
InfiniteData,
QueryClient,
QueryKey,
} from '@tanstack/query-core'
import type { DefinedUseInfiniteQueryResult } from './types'
import type { UseInfiniteQueryOptions } from './types'
import type {
UseSuspenseInfiniteQueryOptions,
UseSuspenseInfiniteQueryResult,
} from './types'

export function useSuspenseInfiniteQuery<
TQueryFnData,
Expand All @@ -18,19 +23,16 @@ export function useSuspenseInfiniteQuery<
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
options: Omit<
UseInfiniteQueryOptions<
TQueryFnData,
TError,
TData,
TQueryFnData,
TQueryKey,
TPageParam
>,
'enabled' | 'suspense' | 'throwOnError' | 'placeholderData'
options: UseSuspenseInfiniteQueryOptions<
TQueryFnData,
TError,
TData,
TQueryFnData,
TQueryKey,
TPageParam
>,
queryClient?: QueryClient,
): Omit<DefinedUseInfiniteQueryResult<TData, TError>, 'isPlaceholderData'> {
): UseSuspenseInfiniteQueryResult<TData, TError> {
return useBaseQuery(
{
...options,
Expand All @@ -41,5 +43,5 @@ export function useSuspenseInfiniteQuery<
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
InfiniteQueryObserver as typeof QueryObserver,
queryClient,
) as DefinedUseInfiniteQueryResult<TData, TError>
) as InfiniteQueryObserverSuccessResult<TData, TError>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think alternative solution is below suggestion

Suggested change
) as InfiniteQueryObserverSuccessResult<TData, TError>
) as unknown as UseSuspenseInfiniteQueryResult<TData, TError>

}