Skip to content

Instantly share code, notes, and snippets.

@leolabs
Created February 19, 2021 14:23
Show Gist options
  • Save leolabs/4c6431989db8470c05fd9f8cb3c44d93 to your computer and use it in GitHub Desktop.
Save leolabs/4c6431989db8470c05fd9f8cb3c44d93 to your computer and use it in GitHub Desktop.
type Falsy = false | 0 | '' | null | undefined;
type NonFalsy<T> = T extends Falsy ? never : T;
/**
* Filter out falsy entries in an array
*/
export function isTruthy<T>(value: T): value is NonFalsy<T> {
return Boolean(value);
}
const array: Array<string | null> = [/* ... */];
array.filter(isTruthy) // Array<string>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment