Skip to content

Commit

Permalink
feat: add invariant and nullthrow
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 21, 2023
1 parent 813d027 commit ca6f799
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/invariant/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @see https://foxact.skk.moe/invariant-nullthrow */
export function invariant<T>(value: T): asserts value is NonNullable<T> {
if (value === null || value === undefined) {
throw new Error('Value is null or undefined');
}
}
7 changes: 7 additions & 0 deletions src/nullthrow/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @see https://foxact.skk.moe/invariant-nullthrow */
export const nullthrow = <T>(value: T, message = 'Value is null or undefined'): NonNullable<T> => {
if (value === null || value === undefined) {
throw new Error(message);
}
return value;
};

0 comments on commit ca6f799

Please sign in to comment.