Skip to content

Instantly share code, notes, and snippets.

@temoncher
Created December 1, 2022 13:16
Show Gist options
  • Select an option

  • Save temoncher/493356702106a4fcb96d5a50c917ef46 to your computer and use it in GitHub Desktop.

Select an option

Save temoncher/493356702106a4fcb96d5a50c917ef46 to your computer and use it in GitHub Desktop.
`isUndefined`, `isDefined` and `complement` typeguards
function complement<A, T extends A>(predicate: (arg: A) => arg is T): (arg: A) => arg is Exclude<A, T>;
function complement<A extends any[]>(predicate: (...args: A) => boolean) {
return (...args: A) => !predicate(...args);
}
function isUndefined<A>(anything: A | undefined): anything is undefined;
function isUndefined(anything: any): anything is undefined {
return anything === undefined
};
const isDefined = complement(isUndefined);
const qqq = [
124,
false ? 11 : undefined,
];
const qq = qqq.filter(isDefined);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment