You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When passing an SWR config of type SWRConfiguration | undefined as an argument to useSWR, data in the swr response is no longer a union type with undefined (regardless of whether the swr config defined suspense: true).
Expected Behavior
I expect data in the below example to be a union type User | undefined because the hook is not running in suspense mode.
Repro Steps / Code Example
Here is a suggested test showcasing the currently failing case:
exportfunctiontestUndefinedData(){constfetcher=(k: string)=>Promise.resolve({value: k})const{ data }=useSWR('/api',fetcher,{}asSWRConfiguration)expectType<Equal<typeofdata,{value: string}|undefined>>(true)}
When doing {} as SWRConfiguration, you're asserting that the value has a different type, I wouldn't expect it to give you the correct type for {} if you assert it to be something else.
Have you tried using {} satisfies SWRConfiguration instead?
Yeah this is a good point. Not a big deal here since we do want to trick typescript into thinking that the {} is of type SWRConfiguration, but satisfies is the safer option 👍.
This PR is merged already though so I'm unfortunately that fix would need to come in a separate PR - please feel free to submit one.
Bug report
Description / Observed Behavior
When passing an SWR config of type
SWRConfiguration | undefined
as an argument touseSWR
,data
in the swr response is no longer a union type withundefined
(regardless of whether the swr config definedsuspense: true
).Expected Behavior
I expect
data
in the below example to be a union typeUser | undefined
because the hook is not running in suspense mode.Repro Steps / Code Example
Here is a suggested test showcasing the currently failing case:
Additional Context
This issue was newly introduced in
2.1.0
.I have confirmed in a local test environment that it was introduced in this PR: https://github.com/vercel/swr/pull/2452/files
The text was updated successfully, but these errors were encountered: