Skip to content

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.

Copy link
Member

@shuding shuding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this idea, thanks!

@shuding shuding merged commit da305b2 into vercel:main Jun 9, 2025
6 checks passed
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
2 participants