From 1e635fedebfe2ec41107a056a2a85bf682802e44 Mon Sep 17 00:00:00 2001 From: Lukasz Matusik Date: Wed, 11 Oct 2023 21:26:31 +0200 Subject: [PATCH] Add PickFromPossiblyUndefined type --- index.d.ts | 1 + readme.md | 1 + source/pick-from-possibly-undefined.d.ts | 19 +++++++++++++++++ test-d/pick-from-possibly-undefined.ts | 26 ++++++++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 source/pick-from-possibly-undefined.d.ts create mode 100644 test-d/pick-from-possibly-undefined.ts diff --git a/index.d.ts b/index.d.ts index f49552fe8..b93720de7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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'; diff --git a/readme.md b/readme.md index 0a66e4fe8..229494631 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/source/pick-from-possibly-undefined.d.ts b/source/pick-from-possibly-undefined.d.ts new file mode 100644 index 000000000..81b0b9073 --- /dev/null +++ b/source/pick-from-possibly-undefined.d.ts @@ -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; + +Results in: UserWithId = { id: number, name: string } | undefined + +@category Object + **/ +export type PickFromPossiblyUndefined> = NonNullable extends object + ? Pick, Props> + : NonNullable; diff --git a/test-d/pick-from-possibly-undefined.ts b/test-d/pick-from-possibly-undefined.ts new file mode 100644 index 000000000..a944d04d2 --- /dev/null +++ b/test-d/pick-from-possibly-undefined.ts @@ -0,0 +1,26 @@ +import {expectAssignable, expectType} from 'tsd'; +import type {PickFromPossiblyUndefined} from '../index'; +import {expectTypeOf} from 'expect-type'; + +type BillingDetails = { + taxId: string; + companyName: string; + address: string; + bankAccount: string; + ibanBankAccount: string; +} | undefined; + +type CompanyBankAccounts = PickFromPossiblyUndefined; + +const bankAccounts: CompanyBankAccounts = { + bankAccount: '123456789', + ibanBankAccount: '123456789', +}; + +expectAssignable(bankAccounts); +expectType(bankAccounts.bankAccount); +expectType(bankAccounts.ibanBankAccount); +expectTypeOf(bankAccounts).toMatchTypeOf({ + bankAccount: '123456789', + ibanBankAccount: '123456789', +});