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
base: main
Are you sure you want to change the base?
Conversation
Please read the contributing guidelines before submitting contribution. |
@Emiyaaaaa you want me to provide real life usage example or what? I've followed guidelines... |
Yes, you need:
|
|
||
type UserWithId = PickFromPossiblyUndefined<User, 'id' | 'name'>; | ||
|
||
Results in: UserWithId = { id: number, name: string } | 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.
Result seems { id: number, name: string }
, not { id: number, name: string } | undefined
.
Did this meet your expectations?
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.
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>; |
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 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
?
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.
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
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.
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
?
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.
Okay. Let's check now, I've also added some validation so one wouldn't just do:
type UndefinedUserAccount = PickFromPossiblyUndefined<undefined, ''>;
need remove .idea folder |
@@ -0,0 +1,23 @@ | |||
/** | |||
It allows to `Pick` properties from a type that may be 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.
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
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.
Now, better?
|
||
@category Object | ||
**/ | ||
export type PickFromPossiblyUndefined<Type, Props extends keyof NonNullable<Type>> = Type extends 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.
What is the different with
type PickFromPossiblyUndefined<Type, Props extends keyof NonNullable<Type>> = Pick<NonNullable<Type>, Props>
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.
it won't work the same (tests would crash).
|
||
@category Object | ||
**/ | ||
export type PickFromPossiblyUndefined<Type, Props extends keyof NonNullable<Type>> = Type extends 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 are you using NonNullable
? We only want undefined
, not undefined
and null
.
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.
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. |
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.
- [`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; |
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.
Tab-indentation.
|
||
@example: | ||
``` | ||
import { PickFromPossiblyUndefined } from 'type-fest'; |
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.
import { PickFromPossiblyUndefined } from 'type-fest'; | |
import {PickFromPossiblyUndefined} from 'type-fest'; |
``` | ||
|
||
@category Object | ||
**/ |
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.
**/ | |
**/ |
Bump :) |
@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. |
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. |
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 :)