Skip to content

Commit

Permalink
add nullable types functions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej-dyck committed Dec 16, 2023
1 parent a803163 commit e04a2d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './json/index.js'
export * from './lazy/index.js'
export * from './locale/index.js'
export * from './nullables/index.js'
export * from './raise/index.js'
export * from './records/index.js'
11 changes: 11 additions & 0 deletions nullables/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Checks whenever a value is not nullable; same as value != null
*
* Useful for Array.filter():
* const arr: (string | undefined)[] = [...]
* ❌ arr.filter(v => v != null) // type: (string | undefined)[]
* ❌ arr.filter(v => typeof v === 'string') // type: (string | undefined)[]
* ✅ arr.filter(isNonNullable) // type: string[]
*/
export const isNonNullable = <T>(value: NonNullable<T> | undefined | null): value is NonNullable<T> =>
value != null

0 comments on commit e04a2d0

Please sign in to comment.