Skip to content

Instantly share code, notes, and snippets.

@luigircruz
Created August 30, 2022 06:09
Show Gist options
  • Select an option

  • Save luigircruz/d9103f20002d7dba72dc8fd3f4e06a3a to your computer and use it in GitHub Desktop.

Select an option

Save luigircruz/d9103f20002d7dba72dc8fd3f4e06a3a to your computer and use it in GitHub Desktop.
/**
* Type guard to filter out null-ish values
*
* @category Guards
* @example array.filter(notNullish)
*/
export function notNullish<T>(v: T | null | undefined): v is NonNullable<T> {
return v != null
}
/**
* Type guard to filter out null values
*
* @category Guards
* @example array.filter(noNull)
*/
export function noNull<T>(v: T | null): v is Exclude<T, null> {
return v !== null
}
/**
* Type guard to filter out null-ish values
*
* @category Guards
* @example array.filter(notUndefined)
*/
export function notUndefined<T>(v: T): v is Exclude<T, undefined> {
return v !== undefined
}
/**
* Type guard to filter out falsy values
*
* @category Guards
* @example array.filter(isTruthy)
*/
export function isTruthy<T>(v: T): v is NonNullable<T> {
return Boolean(v)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment