Skip to content

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

Closed
wants to merge 3 commits into from

Conversation

ferm10n
Copy link

@ferm10n ferm10n commented Jul 22, 2021

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 example

const pickedResult = useResult(result, [], data => data.pickableThings);

To get the types to work, I have to provide values for the generics which is a bit verbose, but fine:

const pickedResult = useResult<Result, PickedResult, PickedResult>(result, [], data => data.pickableThings);

But now that the result.value is correctly typed, it reveals that the type of the data 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.

@ferm10n
Copy link
Author

ferm10n commented Jul 22, 2021

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.

@ferm10n
Copy link
Author

ferm10n commented Jul 24, 2021

I'm assuming the build failure isn't an issue since the target branch's build is still failing too for the same reason

ferm10n added 2 commits July 26, 2021 22:01
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`
@ferm10n ferm10n changed the title types: exclude undefined from useResult pick fn types: exclude null and undefined from useResult pick fn and generics Jul 27, 2021
@skix123
Copy link

skix123 commented Jul 30, 2021

Hi @ferm10n, thanks for this PR! I believe it solves this issue: #1231

Would love to see it merged

@@ -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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why those changes?

@Akryum
Copy link
Member

Akryum commented Sep 20, 2021

Thanks for the PR! I fixed the original issue in a different way in 4475805

@Akryum Akryum closed this Sep 20, 2021
@ferm10n
Copy link
Author

ferm10n commented Sep 21, 2021

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.

@Akryum
Copy link
Member

Akryum commented Sep 21, 2021

That's what the try catch is for

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

Successfully merging this pull request may close these issues.

3 participants