Skip to content

Commit

Permalink
add deep-partial utility type
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej-dyck committed Dec 16, 2023
1 parent c53253a commit 8788bdf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
28 changes: 28 additions & 0 deletions types/DeepPartial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Partial<T>, but recursive for records.
*/
export type DeepPartial<T> =
T extends Record<PropertyKey, unknown>
? { [P in keyof T]?: DeepPartial<T[P]> }
: T

/*
* Example:
* type Contact = {
* id: string,
* email: string,
* address: {
* street: string
* city: string
* }
* }
*
* type Inputs = DeepPartial<Omit<Contact, 'id'>>
* // type: {
* email?: string | undefined
* address?: {
* street?: string | undefined
* city?: string | undefined
* } | undefined
* }
*/
1 change: 1 addition & 0 deletions types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './DeepPartial.js'

0 comments on commit 8788bdf

Please sign in to comment.