swr
swr copied to clipboard
Conditional rendering does not have correct type
Bug report
Description / Observed Behavior
Conditional fetching does not return expected type.
Expected Behavior
The response type should be a union type of the fetcher response type and undefined.
Repro Steps / Code Example
For this example code:
const fetcher: async (): Promise<ResponseType> => {...}
const response = useSWR(shouldFetch ? '/api/data' : null, fetcher)
The type of response is SWRResponse<ResponseType> when it should be SWRResponse<ResponseType | undefined> as when shouldFetch
is true
the data
property in the SWRResponse is undefined.
Additional Context
SWR version. 2.2.5