Skip to content

Instantly share code, notes, and snippets.

@papalardo
Last active January 14, 2025 16:53
Show Gist options
  • Save papalardo/a7030b286dbeb3ddfcf8f1c0aa342d7a to your computer and use it in GitHub Desktop.
Save papalardo/a7030b286dbeb3ddfcf8f1c0aa342d7a to your computer and use it in GitHub Desktop.
export const blank = <T>(
value: T,
): value is Extract<T, null | undefined | '' | [] | Record<string, never>> => {
if (value === undefined || value === null) {
return true
}
if (typeof value === 'boolean') {
return value === false
}
if (typeof value === 'number') {
return value === 0
}
if (typeof value === 'string') {
return value.trim() === ''
}
if (Array.isArray(value)) {
return value.length === 0
}
if (typeof value === 'object' && value !== null) {
return Object.keys(value).length === 0
}
return false
}
export const filled = <T>(value: T): value is NonNullable<T> => !blank(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment