You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is the new or updated feature that you are suggesting?
I think the return type of useAsync hook might be improved. Currently, status doesn't narrow the Result type:
asyncfunctionExample(){const[{ status, result },{ execute }]=useAsync(promise);if(status==='error'){return ...;}if(status==='loading'||status==='not-executed'){return ...;}if(status==='success'){consttest=result.body;// result is possibly `undefined`}}
it would be great if an exhaustive status check was enough to narrow the return type like:
asyncfunctionExample(){const[{ status, result },{ execute }]=useAsync(promise);if(status==='error'){return ...;}if(status==='loading'||status==='not-executed'){return ...;}consttest=result.body;// correctly inferred as `Result`, without `undefined`}
I didn't go very far with this, so I would have to read carefully if the types are consistent with the hook's runtime behavior.
Why should this feature be included?
It will improve a developer experience, as well as the readability.
Let me know what do you think about it - if it looks good - are you open for a PR? 😄
The text was updated successfully, but these errors were encountered:
Hello 👋 Thank you for the awesome library!
New Features
Result
inference inuseAsync
hookWhat is the new or updated feature that you are suggesting?
I think the return type of
useAsync
hook might be improved. Currently,status
doesn't narrow theResult
type:so we have to additionally check if the result exists, even if the status is
success
.I guess it is a result of
Result | undefined
union in a second overload: https://github.com/react-hookz/web/blob/master/src/useAsync/index.ts#L57it would be great if an exhaustive status check was enough to narrow the return type like:
I was inspired by the behavior of React Query and their overloads: https://github.com/TanStack/query/blob/main/packages/react-query/src/useQuery.ts and prepared a simple demo in order to achieve the desired result: master...Onxi95:react-hookz:poc/async-state-discriminated-union
I didn't go very far with this, so I would have to read carefully if the types are consistent with the hook's runtime behavior.
Why should this feature be included?
It will improve a developer experience, as well as the readability.
Let me know what do you think about it - if it looks good - are you open for a PR? 😄
The text was updated successfully, but these errors were encountered: