Skip to content

Instantly share code, notes, and snippets.

@GingerBear
Created September 17, 2019 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GingerBear/b79fffc4e7d44331448dae1306a33b40 to your computer and use it in GitHub Desktop.
Save GingerBear/b79fffc4e7d44331448dae1306a33b40 to your computer and use it in GitHub Desktop.
TS arr filter type guard
function notNullOrUndefined<T>(x: T | null | undefined): x is T {
return x !== null && x !== undefined;
}
const arr = [1, 2, null, undefined]; // (number | null | undefined)[]
const arrFiltered = arr.filter(notNullOrUndefined); // number[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment