Created
December 1, 2022 13:16
-
-
Save temoncher/493356702106a4fcb96d5a50c917ef46 to your computer and use it in GitHub Desktop.
`isUndefined`, `isDefined` and `complement` typeguards
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 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