Skip to content

Commit

Permalink
types: isLoading typed as boolean when using fallbackData (#2866) (#2875
Browse files Browse the repository at this point in the history
)
  • Loading branch information
yjrhgvbn authored Jan 17, 2024
1 parent d5d8482 commit 30cfa8c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/_internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ export type SWRConfiguration<
Fn extends BareFetcher<any> = BareFetcher<any>
> = Partial<PublicConfiguration<Data, Error, Fn>>

export type IsLoadingResponse<
Data = any,
Options = SWROptions<Data>
> = Options extends { suspense: true } ? false : boolean

type SWROptions<Data> = SWRConfiguration<Data, Error, Fetcher<Data, Key>>
export interface SWRResponse<Data = any, Error = any, Config = any> {
/**
Expand All @@ -417,7 +422,7 @@ export interface SWRResponse<Data = any, Error = any, Config = any> {
error: Error | undefined
mutate: KeyedMutator<Data>
isValidating: boolean
isLoading: BlockingData<Data, Config> extends true ? false : boolean
isLoading: IsLoadingResponse<Data, Config>
}

export type KeyLoader<Args extends Arguments = Arguments> =
Expand Down
9 changes: 9 additions & 0 deletions test/type/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,12 @@ export function testEmptyConfig() {
expectType<Equal<typeof error, Error | undefined>>(true)
expectType<Equal<typeof isLoading, boolean>>(true)
}

export function testFallbackDataConfig() {
const fetcher = (k: string) => Promise.resolve({ value: k })
const { data, isLoading } = useSWR('/api', fetcher, {
fallbackData: { value: 'fallback' }
})
expectType<Equal<typeof data, { value: string }>>(true)
expectType<Equal<typeof isLoading, boolean>>(true)
}

0 comments on commit 30cfa8c

Please sign in to comment.