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

typeof 'data' is always optional even with 'fallbackData' provided. #1410

Closed
kjsik11 opened this issue Aug 30, 2021 · 6 comments · Fixed by #2301
Closed

typeof 'data' is always optional even with 'fallbackData' provided. #1410

kjsik11 opened this issue Aug 30, 2021 · 6 comments · Fixed by #2301

Comments

@kjsik11
Copy link

kjsik11 commented Aug 30, 2021

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',
});

image

SWR version.
1.0.0

@kayk1m
Copy link

kayk1m commented Sep 22, 2021

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 swr@1.0.0 (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;
}

@tachibanayu24
Copy link

Is there any update on this issue?

@dnjsgur0629
Copy link

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.

@mrcaidev
Copy link

Still an issue in v2.1.5.

image

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 }.

@BoneTM
Copy link

BoneTM commented Jun 13, 2023

Still an issue in v2.1.5.

image

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

@peterhirn
Copy link

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 })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants