-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Support clarifying SWRResponse type to do type narrowing SWRResponse["data"] by flags(isSuccess) or union type or any other ways please #2482
Comments
I dont think we need Could this do?
|
Then don't we need to provide type narrowing by isLoading, errorbecause of cached data by swr, we don't have to type narrowing by isValidating. I expected |
Yea true! Im not sure why data can be undefined in that case 🤔 useSWR hook must do something. Ill keep looking! |
@tobbbe @manudeli I'm interested in this feature I like what I see in #2175 Do you know if there is something missing here or edge cases that are not covered? Also for more context about this feature, this is a good comparison with react-query https://news.ycombinator.com/item?id=33929553 |
How can we use useSWR like below to do type narrowing for data?
but in this case, if fetcher to put in useSWR return undefined, how can we do guarantee fetcher's returning promise is success? you can check it this edge case. data is never.
I'm sad that there is no isSuccess flag in useSWR because of this problem.
Could you make it TypeScript example to guide us avoiding this problem?
In this example(https://swr.vercel.app/examples/basic) in official guide in JavaScript file. I can understand it because of JavaScript will accept it. but in TypeScript is not!🥲
In JavaScript, no type error
In TypeScript, Type error will be occured
How do we have to avoid this type error situation?
Originally posted by @manudeli in #2175 (comment)
What I expected: union return type of useSWR containing flag expressing Promise's status
in @tanstack/react-query, they support union return type of useQuery, so we can do type narrowing by isSuccess, isLoading, isError.
but in swr, it's not.
type narrowing by isSuccess in @tanstack/react-query
type narrowing by isLoading, isError in @tanstack/react-query
The text was updated successfully, but these errors were encountered: