Skip to content

Instantly share code, notes, and snippets.

@etienne-dldc
Created January 21, 2020 10:04
Show Gist options
  • Save etienne-dldc/523a290476ddc78918f101289c6a2a99 to your computer and use it in GitHub Desktop.
Save etienne-dldc/523a290476ddc78918f101289c6a2a99 to your computer and use it in GitHub Desktop.
Invariant in TypeScript
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