-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
# Bug report
## Description / Observed Behavior
Typescript is not properly inferring the correct type, when suspense is passed as a parameter. It works right when suspense is switch true/false inside the configuration. However it can't infer the types if you pass suspense as a parameter via an intermediate function
What kind of issues did you encounter with SWR?
This forces me to use unnecessary checks, or casting.
## Expected Behavior
How did you expect SWR to behave here?
I expected the types to correctly infer the data type, removing 'undefined' if it receives suspense: true.
## Repro Steps / Code Example
JYWwDg9gTgLgBAVwM4FMDKB1ASnAZlCEOAciQHcpiBuAKDpQA9JY4BjCAOyXmRQFVUUOAF44ACiTIwKLigBccAEYQIAGxQBDDgEoRAPjgBvGnDhQUMBFA6JUmLGIBEAaRQBPRwBpxu4QcNmFlY2HChkcAAKBCDAqAA83FDAHADmemJi5khqAG4ovgaoMAAqoCgQCDAZBYHZqnlOABIoqqoQcGTQqgAmjtreAEwArAAMI9q6AL7eAZJI0rJwk9o0k
import useSWR from 'swr';
export const useUser = (suspense: boolean) => {
return useSWR("Key", () => { return new Promise((resolve) => setTimeout(() => resolve("Hello world"), 2500)) }, { suspense })
}
export const useUserWithSuspense = () => {
return useSWR("Key", () => { return new Promise((resolve) => setTimeout(() => resolve("Hello world"), 2500)) }, { suspense: true })
}
const App = () => {
const { data } = useUser(true);
// ^?
const { data } = useUser(false);
// ^?
const { data } = useUserWithSuspense();
// ^?
}