swr
swr copied to clipboard
typeof 'data' is always optional even with 'fallbackData' provided.
Bug report
Description / Observed Behavior
type of data
Expected Behavior
type of data
should be Data
(not Data | undefined
) if fallbackData
has provided in type Data
.
Repro Steps / Code Example
const { data } = useSWR<string>('my-key', {
fallbackData: 'hello world',
});
SWR version. 1.0.0
Any plans to resolve this issue?
It's so confusing especially while I am using Shared Hook State with SWR which is updated feature at [email protected]
(using undefined
for fetcher
)
for example use case,
export function useEmail() {
const { data: email, mutate: setEmail } = useSWR('@email-key', {
fallbackData: '',
fetcher: undefined,
});
return [email!, setEmail] as const;
}
Is there any update on this issue?
It is weird that data can still be undefined even though fallbackData is set. Because of this issue, it is quite inconvenient to use useSWR and typeScript together.
Still an issue in v2.1.5.
Anyway coercion is always a workaround, but it is still strange because #2301 seems to have fixed this with BlockingData
, and my config parameter does extend { fallbackData: Data }
.
Still an issue in v2.1.5.
Anyway coercion is always a workaround, but it is still strange because #2301 seems to have fixed this with
BlockingData
, and my config parameter does extend{ fallbackData: Data }
.
dont specify generic, it need fetcher and fallbackData to infer the type
dont specify generic, it need fetcher and fallbackData to infer the type
Thanks for the info! It works:
// string | undefined
const { data } = useSWR<string>("/api", { suspense: true })
// string
const { data } = useSWR("/api", fetcher<string>, { suspense: true })