Arrays
Type guard functions for filtering arrays.
notNull
Returns true if the value is not null and not undefined. Narrows the type accordingly.
import { notNull } from '@repo/utils'
function notNull<T>(value: T | null | undefined): value is T
Example
import { notNull } from '@repo/utils'
const ids: (number | null)[] = [1, null, 2, null, 3]
const valid = ids.filter(notNull)
// valid: number[] = [1, 2, 3]
Useful with .map() + .filter() chains:
import { notNull } from '@repo/utils'
const users = [{ name: 'Alice' }, { name: undefined }, { name: 'Bob' }]
const names = users.map(u => u.name).filter(notNull)
// names: string[] = ['Alice', 'Bob']
notEmpty
Returns true if the value is not null, not undefined, and truthy. Filters out empty strings, 0, false, etc.
import { notEmpty } from '@repo/utils'
function notEmpty<T>(value: T | null | undefined): value is T
Example
import { notEmpty } from '@repo/utils'
const values = ['hello', '', null, 'world', undefined, 0]
const result = values.filter(notEmpty)
// result: (string | number)[] = ['hello', 'world']
// Note: 0 and '' are filtered out because they are falsy