-
-
Notifications
You must be signed in to change notification settings - Fork 523
types: exclude null and undefined from useResult pick fn and generics #1233
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
Conversation
P.S. I still feel like I'm learning how to write good PR descriptions. I'd appreciate if there's any feedback on how I wrote this. Mainly concerned about being too wordy, or not giving enough info that's helpful. |
I'm assuming the build failure isn't an issue since the target branch's build is still failing too for the same reason |
this allows you to not have to write useResult<ResultType | undefined>(...), because we can assume the result type is undefined until it is fetched. If the result type already has undefined, then this is a noop and should be fine.
now it'll work better with useSubscription too, since that is typed as `TResult | null | undefined`
@@ -17,7 +17,7 @@ export type UseResultReturn<T> = Readonly<Ref<Readonly<T>>> | |||
* @returns Readonly ref with `undefined` or the resolved `result`. | |||
*/ | |||
export function useResult<TResult, TResultKey extends keyof NonNullable<TResult> = keyof NonNullable<TResult>> ( | |||
result: Ref<TResult> | |||
result: Ref<TResult | null | undefined> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why those changes?
Thanks for the PR! I fixed the original issue in a different way in 4475805 |
Thanks for taking a look at this. I'm concerned with the DeepNonNullable type though. Isn't it possible for a query to return a null somewhere nested in the response? I haven't verified this yet this is actually an issue yet though. |
That's what the try catch is for |
I was excited to learn about #1062 which fixed the potentially undefined result.value, but ran into an issue with the types in
useResult()
. Here's the exampleTo get the types to work, I have to provide values for the generics which is a bit verbose, but fine:
But now that the
result.value
is correctly typed, it reveals that the type of thedata
argument in the pick function needs to be fixed. The pick function will never be called if its value is undefined, so data should exclude undefined from its type. This PR is just a small change to implement that.