Skip to content

Data cannot be undefined when suspense = true #266

Closed
@Svish

Description

@Svish

Wanted to try enabling Suspense Mode in a project today, and ran into a typing issue. The responseInterface of useSWR is typed like this:

export type responseInterface<Data, Error> = {
  data?: Data
  error?: Error
  revalidate: () => Promise<boolean>
  isValidating: boolean
}

In Suspense mode, data is always the fetch response (so you don't need to check if it's undefined). But if an error occurred, you need to use an error boundary to catch it.

But I do need to check if it's undefined. 😕

I guess the interface in Suspense Mode should be more like this?

export type responseInterface<Data> = {
  data: Data
  revalidate: () => Promise<boolean>
  isValidating: boolean // <-- Assuming this is still useful for revalidation
}

Not sure how to best solve this though, but it should somehow be solved. I don't feel like starting to throw ! or // @ts-ignore stuff everywhere I use useSWR with suspense mode. 🤔

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions