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

Add PickFromPossiblyUndefined type #703

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Proxxx23
Copy link

@Proxxx23 Proxxx23 commented Oct 11, 2023

I've stumbled upon a problem with Picking from a type that may be undefined. That's why I've decided to make my own type (its name may be changed ofc) and also add it to this fantastic library as a contribution :)

@Emiyaaaaa
Copy link
Collaborator

Emiyaaaaa commented Oct 18, 2023

Please read the contributing guidelines before submitting contribution.

@Proxxx23
Copy link
Author

@Emiyaaaaa you want me to provide real life usage example or what? I've followed guidelines...

@Emiyaaaaa
Copy link
Collaborator

provide real life usage example

Yes, you need:

Write about some real-world use-cases where it can be useful. (It can be hard sometimes for users to see where they would use something)


type UserWithId = PickFromPossiblyUndefined<User, 'id' | 'name'>;

Results in: UserWithId = { id: number, name: string } | undefined
Copy link
Collaborator

@Emiyaaaaa Emiyaaaaa Oct 24, 2023

Choose a reason for hiding this comment

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

Result seems { id: number, name: string }, not { id: number, name: string } | undefined.
Did this meet your expectations?

Copy link
Author

@Proxxx23 Proxxx23 Oct 24, 2023

Choose a reason for hiding this comment

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

Yes, sorry, you're right. Fixed that one.

**/
export type PickFromPossiblyUndefined<Type, Props extends keyof NonNullable<Type>> = NonNullable<Type> extends object
? Pick<NonNullable<Type>, Props>
: NonNullable<Type>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why return a nonNullable version of Type? I think this type should have the same behaviour as Pick except not object. or your type more like SafePick ?

Copy link
Author

Choose a reason for hiding this comment

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

It won't work, TS will yell at you if you try to just Pick<Type, Props>. Using NonNullable allows you to pick from possibly undefined, that's the case. Otherwise, you'll see

TS2345: Argument of type  unknown  is not assignable to parameter of type  string

Copy link
Collaborator

Choose a reason for hiding this comment

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

It won't work, TS will yell at you if you try to just Pick. Using NonNullable allows you to pick from possibly undefined, that's the case. Otherwise, you'll see

TS2345: Argument of type  unknown  is not assignable to parameter of type  string

sorry, I mean 'Why return NonNullable<Type> when NonNullable<Type> not extends object?'

Would it be better to return never?

Choose a reason for hiding this comment

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

Okay. Let's check now, I've also added some validation so one wouldn't just do:

type UndefinedUserAccount = PickFromPossiblyUndefined<undefined, ''>;

@Emiyaaaaa
Copy link
Collaborator

need remove .idea folder

@@ -0,0 +1,23 @@
/**
It allows to `Pick` properties from a type that may be undefined

Copy link
Collaborator

Choose a reason for hiding this comment

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

need add Use-case

Write about some real-world use-cases where it can be useful. (It can be hard sometimes for users to see where they would use something)
https://github.com/sindresorhus/type-fest/blob/main/.github/contributing.md

Copy link
Author

Choose a reason for hiding this comment

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

Now, better?

readme.md Show resolved Hide resolved

@category Object
**/
export type PickFromPossiblyUndefined<Type, Props extends keyof NonNullable<Type>> = Type extends undefined
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the different with

type PickFromPossiblyUndefined<Type, Props extends keyof NonNullable<Type>> = Pick<NonNullable<Type>, Props>

Copy link
Author

Choose a reason for hiding this comment

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

it won't work the same (tests would crash).


@category Object
**/
export type PickFromPossiblyUndefined<Type, Props extends keyof NonNullable<Type>> = Type extends undefined
Copy link
Owner

Choose a reason for hiding this comment

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

Why are you using NonNullable? We only want undefined, not undefined and null.

Copy link
Author

Choose a reason for hiding this comment

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

How would you do that?

@@ -144,6 +144,7 @@ Click the type names for complete docs.
- [`ConditionalKeys`](source/conditional-keys.d.ts) - Extract keys from a shape where values extend the given `Condition` type.
- [`ConditionalPick`](source/conditional-pick.d.ts) - Like `Pick` except it selects properties from a shape where the values extend the given `Condition` type.
- [`ConditionalPickDeep`](source/conditional-pick-deep.d.ts) - Like `ConditionalPick` except that it selects the properties deeply.
- [`PickFromPossiblyUndefined`](source/pick-from-possibly-undefined.d.ts) - `Pick` properties from type that may be undefined.
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
- [`PickFromPossiblyUndefined`](source/pick-from-possibly-undefined.d.ts) - `Pick` properties from type that may be undefined.
- [`PickFromPossiblyUndefined`](source/pick-from-possibly-undefined.d.ts) - Like `Pick` except it allows a type that is possibly `undefined`.

import { PickFromPossiblyUndefined } from 'type-fest';

type BillingDetails = {
taxId: string;
Copy link
Owner

Choose a reason for hiding this comment

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

Tab-indentation.


@example:
```
import { PickFromPossiblyUndefined } from 'type-fest';
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
import { PickFromPossiblyUndefined } from 'type-fest';
import {PickFromPossiblyUndefined} from 'type-fest';

```

@category Object
**/
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
**/
**/

@sindresorhus
Copy link
Owner

Bump :)

@Proxxx23
Copy link
Author

@sindresorhus I don't have this code locally nor in my forked repo. Can I close this PR and make new one? I'll just move this code to another PR and create PR one more time.

@sindresorhus
Copy link
Owner

No need to open a new pull request. The code is right here: https://github.com/Proxxx23/type-fest/tree/main You can just clone it.

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.

None yet

4 participants