Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWR should assert the data not be undefined if isLoading is false and error is null #2943

Open
cw1997 opened this issue Apr 20, 2024 · 1 comment

Comments

@cw1997
Copy link

cw1997 commented Apr 20, 2024

The sample code at here:

const {data, error, isLoading} = useSWR(['passport/session/get'], () => get_session())
if (!isLoading && error == null) {
  console.log(data.user_id)
}

image

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 #2942

@cw1997
Copy link
Author

cw1997 commented Apr 24, 2024

ref: #2482

#2175

@cw1997 cw1997 changed the title SWR should assert the data must not be undefined if isLoading is false and error is null SWR should assert the data not be undefined if isLoading is false and error is null Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant