Skip to content

Commit

Permalink
refactor(query-core): type override subscribe in InfiniteQueryObserve…
Browse files Browse the repository at this point in the history
…r more clearly (#7914)

* fix(query-core): type override subscribe in InfiniteQueryObserver more clearly

* chore: update
  • Loading branch information
manudeli authored Aug 19, 2024
1 parent c7fb5fd commit 683c85e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
38 changes: 30 additions & 8 deletions packages/query-core/src/infiniteQueryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
hasPreviousPage,
infiniteQueryBehavior,
} from './infiniteQueryBehavior'
import type { Subscribable } from './subscribable'
import type {
DefaultError,
DefaultedInfiniteQueryObserverOptions,
Expand All @@ -16,7 +17,7 @@ import type {
QueryKey,
} from './types'
import type { QueryClient } from './queryClient'
import type { NotifyOptions, ObserverFetchOptions } from './queryObserver'
import type { NotifyOptions } from './queryObserver'
import type { Query } from './query'

type InfiniteQueryObserverListener<TData, TError> = (
Expand All @@ -38,17 +39,33 @@ export class InfiniteQueryObserver<
TQueryKey
> {
// Type override
subscribe!: (
listener?: InfiniteQueryObserverListener<TData, TError>,
) => () => void
subscribe!: Subscribable<
InfiniteQueryObserverListener<TData, TError>
>['subscribe']

// Type override
getCurrentResult!: () => InfiniteQueryObserverResult<TData, TError>
getCurrentResult!: ReplaceReturnType<
QueryObserver<
TQueryFnData,
TError,
TData,
InfiniteData<TQueryData, TPageParam>,
TQueryKey
>['getCurrentResult'],
InfiniteQueryObserverResult<TData, TError>
>

// Type override
protected fetch!: (
fetchOptions: ObserverFetchOptions,
) => Promise<InfiniteQueryObserverResult<TData, TError>>
protected fetch!: ReplaceReturnType<
QueryObserver<
TQueryFnData,
TError,
TData,
InfiniteData<TQueryData, TPageParam>,
TQueryKey
>['fetch'],
Promise<InfiniteQueryObserverResult<TData, TError>>
>

constructor(
client: QueryClient,
Expand Down Expand Up @@ -176,3 +193,8 @@ export class InfiniteQueryObserver<
return result as InfiniteQueryObserverResult<TData, TError>
}
}

type ReplaceReturnType<
TFunction extends (...args: Array<any>) => unknown,
TReturn,
> = (...args: Parameters<TFunction>) => TReturn
2 changes: 1 addition & 1 deletion packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface NotifyOptions {
listeners?: boolean
}

export interface ObserverFetchOptions extends FetchOptions {
interface ObserverFetchOptions extends FetchOptions {
throwOnError?: boolean
}

Expand Down

0 comments on commit 683c85e

Please sign in to comment.