From e5b727987f79973ab83c210b8fb30a4c61659782 Mon Sep 17 00:00:00 2001 From: Andrej Dyck <43913051+andrej-dyck@users.noreply.github.com> Date: Tue, 23 Jul 2024 10:33:49 +0200 Subject: [PATCH] add optional utility type --- types/Optional.ts | 18 ++++++++++++++++++ types/index.ts | 1 + 2 files changed, 19 insertions(+) create mode 100644 types/Optional.ts diff --git a/types/Optional.ts b/types/Optional.ts new file mode 100644 index 0000000..2929cc0 --- /dev/null +++ b/types/Optional.ts @@ -0,0 +1,18 @@ +/** + * Makes some properties of a record optional. + */ +export type Optional> = Omit & { [K in keyof T]?: T[K] } + +/* + * Example: + * type CartItem = { + * id: string, + * sku: string, + * quantity: number, + * } + * + * function addItem(item: Optional) { + * // typeof item: { id?: string, sku: string, quantity?: number } + * ... + * } + */ diff --git a/types/index.ts b/types/index.ts index efe43e2..02cd181 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1,2 +1,3 @@ export * from './Branded.ts' export * from './DeepPartial.ts' +export * from './Optional.ts'