Skip to content

Commit 30cfa8c

Browse files
authored
types: isLoading typed as boolean when using fallbackData (#2866) (#2875)
1 parent d5d8482 commit 30cfa8c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/_internal/types.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ export type SWRConfiguration<
405405
Fn extends BareFetcher<any> = BareFetcher<any>
406406
> = Partial<PublicConfiguration<Data, Error, Fn>>
407407

408+
export type IsLoadingResponse<
409+
Data = any,
410+
Options = SWROptions<Data>
411+
> = Options extends { suspense: true } ? false : boolean
412+
408413
type SWROptions<Data> = SWRConfiguration<Data, Error, Fetcher<Data, Key>>
409414
export interface SWRResponse<Data = any, Error = any, Config = any> {
410415
/**
@@ -417,7 +422,7 @@ export interface SWRResponse<Data = any, Error = any, Config = any> {
417422
error: Error | undefined
418423
mutate: KeyedMutator<Data>
419424
isValidating: boolean
420-
isLoading: BlockingData<Data, Config> extends true ? false : boolean
425+
isLoading: IsLoadingResponse<Data, Config>
421426
}
422427

423428
export type KeyLoader<Args extends Arguments = Arguments> =

test/type/config.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,12 @@ export function testEmptyConfig() {
148148
expectType<Equal<typeof error, Error | undefined>>(true)
149149
expectType<Equal<typeof isLoading, boolean>>(true)
150150
}
151+
152+
export function testFallbackDataConfig() {
153+
const fetcher = (k: string) => Promise.resolve({ value: k })
154+
const { data, isLoading } = useSWR('/api', fetcher, {
155+
fallbackData: { value: 'fallback' }
156+
})
157+
expectType<Equal<typeof data, { value: string }>>(true)
158+
expectType<Equal<typeof isLoading, boolean>>(true)
159+
}

0 commit comments

Comments
 (0)