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
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type {ConditionalExcept} from './source/conditional-except';
export type {ConditionalKeys} from './source/conditional-keys';
export type {ConditionalPick} from './source/conditional-pick';
export type {ConditionalPickDeep, ConditionalPickDeepOptions} from './source/conditional-pick-deep';
export type {PickFromPossiblyUndefined} from './source/pick-from-possibly-undefined';
export type {UnionToIntersection} from './source/union-to-intersection';
export type {Stringified} from './source/stringified';
export type {FixedLengthArray} from './source/fixed-length-array';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Proxxx23 marked this conversation as resolved.
Show resolved Hide resolved
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`.

- [`ConditionalExcept`](source/conditional-except.d.ts) - Like `Omit` except it removes properties from a shape where the values extend the given `Condition` type.
- [`UnionToIntersection`](source/union-to-intersection.d.ts) - Convert a union type to an intersection type.
- [`LiteralToPrimitive`](source/literal-to-primitive.d.ts) - Convert a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) to the [primitive type](source/primitive.d.ts) it belongs to.
Expand Down
19 changes: 19 additions & 0 deletions source/pick-from-possibly-undefined.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
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?

@example:
type User = {
Proxxx23 marked this conversation as resolved.
Show resolved Hide resolved
id: number;
name: string;
email: string;
} | undefined;

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

Results in: UserWithId = { id: number, name: string } | undefined
Proxxx23 marked this conversation as resolved.
Show resolved Hide resolved
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.


@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
**/
**/

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, ''>;

26 changes: 26 additions & 0 deletions test-d/pick-from-possibly-undefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {expectAssignable, expectType} from 'tsd';
import {expectTypeOf} from 'expect-type';
import type {PickFromPossiblyUndefined} from '../index';

type BillingDetails = {
taxId: string;
companyName: string;
address: string;
bankAccount: string;
ibanBankAccount: string;
} | undefined;

type CompanyBankAccounts = PickFromPossiblyUndefined<BillingDetails, 'bankAccount' | 'ibanBankAccount'>;

const bankAccounts: CompanyBankAccounts = {
bankAccount: '123456789',
ibanBankAccount: '123456789',
};

expectAssignable<CompanyBankAccounts>(bankAccounts);
expectType<string>(bankAccounts.bankAccount);
expectType<string>(bankAccounts.ibanBankAccount);
expectTypeOf(bankAccounts).toMatchTypeOf({
bankAccount: '123456789',
ibanBankAccount: '123456789',
});