Skip to content

Commit

Permalink
Add PickFromPossiblyUndefined type
Browse files Browse the repository at this point in the history
  • Loading branch information
Proxxx23 committed Oct 11, 2023
1 parent 47626cf commit e4041b0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
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.
- [`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
@example:
type User = {
id: number;
name: string;
email: string;
} | undefined;
type UserWithId = PickFromPossiblyUndefined<User, 'id' | 'name'>;
Results in: UserWithId = { id: number, name: string } | undefined
@category Object
**/
export type PickFromPossiblyUndefined<Type, Props extends keyof NonNullable<Type>> = NonNullable<Type> extends object
? Pick<NonNullable<Type>, Props>
: NonNullable<Type>;
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',

Check failure on line 24 in test-d/pick-from-possibly-undefined.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Expected indentation of 1 tab but found 4 spaces.

Check failure on line 24 in test-d/pick-from-possibly-undefined.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected indentation of 1 tab but found 4 spaces.

Check failure on line 24 in test-d/pick-from-possibly-undefined.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

Expected indentation of 1 tab but found 4 spaces.
ibanBankAccount: '123456789',

Check failure on line 25 in test-d/pick-from-possibly-undefined.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Expected indentation of 1 tab but found 4 spaces.

Check failure on line 25 in test-d/pick-from-possibly-undefined.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected indentation of 1 tab but found 4 spaces.

Check failure on line 25 in test-d/pick-from-possibly-undefined.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

Expected indentation of 1 tab but found 4 spaces.
});

0 comments on commit e4041b0

Please sign in to comment.