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

Type-narrowing doesn't work #3055

Open
lcswillems opened this issue Dec 17, 2024 · 8 comments
Open

Type-narrowing doesn't work #3055

lcswillems opened this issue Dec 17, 2024 · 8 comments

Comments

@lcswillems
Copy link

lcswillems commented Dec 17, 2024

Bug report

Description / Observed Behavior

const { data, error, isLoading } = useSWR<number>('/api/user/123', fetcher);

if (!isLoading && !error) {
  data // number | undefined
}

Expected Behavior

It should be of type number.

@Ram4GB
Copy link
Contributor

Ram4GB commented Dec 17, 2024

I don't think useSWR returns isSuccess. Are you mistyping something @lcswillems ?

@lcswillems
Copy link
Author

@Ram4GB My bad. Fixed the example!

@Ram4GB
Copy link
Contributor

Ram4GB commented Dec 18, 2024

I assume you request the user who is not found in your database or has been deleted by other actions. In this case, the API must throw the error with the status 400, so you won't access your data, and it has to return undefined. @lcswillems

@lcswillems
Copy link
Author

Not sure to understand. I'm just saying if "isLoading" is false, then "data" should become of type "number".

It is a basic functionality of TanStack query: https://tanstack.com/query/latest/docs/framework/react/typescript#type-narrowing

@icyJoseph
Copy link
Contributor

But if fetching fails/errors, there won't be data, and it won't be loading either.

@lcswillems
Copy link
Author

@icyJoseph Updated my example. Makes sense? Basically I'm asking for the same thing than TanStack Query.

@Ram4GB
Copy link
Contributor

Ram4GB commented Dec 19, 2024

Yes @lcswillems, your example is not clear to me. In your case, I believe we should get the type number instead of number | undefined.

@lcswillems
Copy link
Author

Yes we should get the type number, it is what I say in the "expected behavior". Currently, the type is number | undefined (Observed behavior).

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

3 participants