Created
September 17, 2019 15:11
-
-
Save GingerBear/b79fffc4e7d44331448dae1306a33b40 to your computer and use it in GitHub Desktop.
TS arr filter type guard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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