Skip to content

Instantly share code, notes, and snippets.

@mrsarm
Created February 13, 2025 14:17
Show Gist options
  • Save mrsarm/823e2fd9e015b3688abd7f89474abff8 to your computer and use it in GitHub Desktop.
Save mrsarm/823e2fd9e015b3688abd7f89474abff8 to your computer and use it in GitHub Desktop.
isDefined: Return true only if the element is not null or undefined
/**
* Return true only if the element is not null or undefined,
* use it with `Array.filter` to not only filter nullable values
* but also to guard the type.
*/
export const isDefined = <T>(argument: T | undefined | null): argument is T => {
return argument !== undefined && argument !== null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment