From 8788bdf6fe7d18a312ec6091e64856591650ac23 Mon Sep 17 00:00:00 2001 From: Andrej Dyck <43913051+andrej-dyck@users.noreply.github.com> Date: Sat, 16 Dec 2023 17:22:33 +0100 Subject: [PATCH] add deep-partial utility type --- index.ts | 1 + types/DeepPartial.ts | 28 ++++++++++++++++++++++++++++ types/index.ts | 1 + 3 files changed, 30 insertions(+) create mode 100644 types/DeepPartial.ts create mode 100644 types/index.ts diff --git a/index.ts b/index.ts index a0b96a1..c956aa0 100644 --- a/index.ts +++ b/index.ts @@ -5,3 +5,4 @@ export * from './nullables/index.js' export * from './raise/index.js' export * from './records/index.js' export * from './sequences/index.js' +export * from './types/index.js' diff --git a/types/DeepPartial.ts b/types/DeepPartial.ts new file mode 100644 index 0000000..ced307c --- /dev/null +++ b/types/DeepPartial.ts @@ -0,0 +1,28 @@ +/** + * Partial, but recursive for records. + */ +export type DeepPartial = + T extends Record + ? { [P in keyof T]?: DeepPartial } + : T + +/* + * Example: + * type Contact = { + * id: string, + * email: string, + * address: { + * street: string + * city: string + * } + * } + * + * type Inputs = DeepPartial> + * // type: { + * email?: string | undefined + * address?: { + * street?: string | undefined + * city?: string | undefined + * } | undefined + * } + */ diff --git a/types/index.ts b/types/index.ts new file mode 100644 index 0000000..0059ef5 --- /dev/null +++ b/types/index.ts @@ -0,0 +1 @@ +export * from './DeepPartial.js'