swr icon indicating copy to clipboard operation
swr copied to clipboard

Conditional rendering does not have correct type

Open benpierce-tumelo opened this issue 1 month ago • 0 comments

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

benpierce-tumelo avatar May 22 '24 08:05 benpierce-tumelo