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 all commits
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
yarn.lock
.idea
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
32 changes: 32 additions & 0 deletions source/pick-from-possibly-undefined.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
`Pick` properties from 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:
```
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';


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.

companyName: string;
address: string;
bankAccount: string;
ibanBankAccount: string;
} | undefined;

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

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

await proceedPayment(bankAccounts);
```

@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>> = 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).

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?

? never
: NonNullable<Type> extends object
? Pick<NonNullable<Type>, Props>
: never;
53 changes: 53 additions & 0 deletions test-d/pick-from-possibly-undefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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',
});

// Test what happens if type to pick from won't be undefined

type AdminAccount = {
id: number;
roleId: number;
firstName: string;
lastName: string;
permissions: string[];
};

type UserAccount = PickFromPossiblyUndefined<AdminAccount, 'id' | 'firstName' | 'lastName'>;

const userAccount: UserAccount = {
id: 1234,
firstName: 'Foo',
lastName: 'Bar',
};

expectAssignable<UserAccount>(userAccount);
expectType<string>(userAccount.firstName);
expectType<string>(userAccount.lastName);
expectTypeOf(userAccount).toMatchTypeOf({
id: 1234,
firstName: 'Foo',
lastName: 'Bar',
});