Created
January 21, 2020 10:04
-
-
Save etienne-dldc/523a290476ddc78918f101289c6a2a99 to your computer and use it in GitHub Desktop.
Invariant in TypeScript
This file contains 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 notNull<T>(val: T | null): T { | |
if (val === null) { | |
throw new Error(`Invariant: value should not be null`); | |
} | |
return val; | |
} | |
function notNil<T>(val: T | null | undefined): T { | |
if (val === null || val === undefined) { | |
throw new Error(`Invariant: value should not be null | undefined`); | |
} | |
return val; | |
} | |
function notUndefined<T>(val: T | undefined): T { | |
if (val === undefined) { | |
throw new Error(`Invariant: value should not be undefined`); | |
} | |
return val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment