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

feat(types): clarify SWRResponse type #2175

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions _internal/types.ts
Expand Up @@ -336,13 +336,22 @@ export type SWRConfiguration<
Fn extends BareFetcher<any> = BareFetcher<any>
> = Partial<PublicConfiguration<Data, Error, Fn>>

export interface SWRResponse<Data = any, Error = any> {
data: Data | undefined
error: Error | undefined
mutate: KeyedMutator<Data>
export type SWRResponse<Data = any, Error = any> = { mutate: KeyedMutator<Data> } & ({
data: undefined
error: undefined
isValidating: true
isLoading: true
} | {
data: Data
error: undefined
isValidating: boolean
isLoading: boolean
}
isLoading: false
} | {
data: undefined
error: Error
isValidating: boolean
isLoading: false
})

export type KeyLoader<Args extends Arguments = Arguments> =
| ((index: number, previousPageData: any | null) => Args)
Expand Down
3 changes: 1 addition & 2 deletions infinite/types.ts
Expand Up @@ -34,8 +34,7 @@ export interface SWRInfiniteConfiguration<
fetcher?: Fn
}

export interface SWRInfiniteResponse<Data = any, Error = any>
extends SWRResponse<Data[], Error> {
export type SWRInfiniteResponse<Data = any, Error = any> = SWRResponse<Data[], Error> & {
size: number
setSize: (
size: number | ((_size: number) => number)
Expand Down