Skip to content

Commit

Permalink
add optional utility type
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej-dyck committed Jul 23, 2024
1 parent 3890ce1 commit e5b7279
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions types/Optional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Makes some properties of a record optional.
*/
export type Optional<T, K extends keyof Record<PropertyKey, unknown>> = Omit<T, K> & { [K in keyof T]?: T[K] }

/*
* Example:
* type CartItem = {
* id: string,
* sku: string,
* quantity: number,
* }
*
* function addItem(item: Optional<CartItem, 'id' | 'quantity'>) {
* // typeof item: { id?: string, sku: string, quantity?: number }
* ...
* }
*/
1 change: 1 addition & 0 deletions types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './Branded.ts'
export * from './DeepPartial.ts'
export * from './Optional.ts'

0 comments on commit e5b7279

Please sign in to comment.