8000 feat: Improve global suspense-enabled `data` type by oosawy · Pull Request #4126 · vercel/swr · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Improve global suspense-enabled data type #4126

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

oosawy
Copy link
Contributor
@oosawy oosawy commented May 5, 2025

As adding suspense: true directly to the useSWR call removes the undefined from the data type, enabling more precise type inference. However, this only works when suspense: true is explicitly passed to each useSWR call — it does not take effect when configured globally via <SWRConfig value={{ suspense: true }}>.

// just using swr or even with global configuration
const { data } = useSWR<Data>('/key', fetcher)
//      ^ Data | undefined

const { data } = useSWR<Data>('/key', fetcher, { suspense: true })
//      ^ Data

This PR add the new SWRGlobalConfig interface allows customizing type inference through declaration merging:

declare module 'swr' {
  interface SWRGlobalConfig {
    suspense: true
  }
}

const { data } = useSWR<Data>('/key', fetcher)
//      ^ Data

As Suspense becomes more common, we want type inference to stay accurate.

Ref #266 and closes #1412

@oosawy oosawy requested review from shuding and huozhi as code owners May 5, 2025 07:49
Copy link
codesandbox-ci bot commented May 5, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

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

Successfully merging this pull request may close these issues.

Better Typing support when suspense is enabled
1 participant
0