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(query-core): type override subscribe in InfiniteQueryObserver more clearly #7914

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
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']
Comment on lines +42 to +44
Copy link
Contributor Author

@manudeli manudeli Aug 18, 2024

Choose a reason for hiding this comment

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

It's trivial. but when doing type overriding, I want to use Subscribable and make it clearer to override while keeping the constraints of Subscribable, so I think it reduces the possibility of making constraint mistakes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldn't we do the same for other type overrides, like fetch a couple of lines below as well?

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 tried this on other type overrides(getCurrentResult, fetch) in ccb4a62


// 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 {
TkDodo marked this conversation as resolved.
Show resolved Hide resolved
throwOnError?: boolean
}

Expand Down
Loading