swr
swr copied to clipboard
SWR should assert the data not be undefined if `isLoading` is false and `error` is null
The sample code at here:
const {data, error, isLoading} = useSWR(['passport/session/get'], () => get_session())
if (!isLoading && error == null) {
console.log(data.user_id)
}
In my opinion, I think if isLoading is false that means fetcher is executed finished, and if error is null, that means the executed result is successful, so data is must not undefined.
But I see the TypeScript report the data may be undefined in this case, is there any way to optimize the logic of typing narrow? Actually, I think in this condition, SWR should assert the data must not be undefined, is this an issue that can be improved?
Thank you very much!
Originally posted by @cw1997 in https://github.com/vercel/swr/discussions/2942
ref: https://github.com/vercel/swr/issues/2482
https://github.com/vercel/swr/pull/2175