aboutsummaryrefslogtreecommitdiff
path: root/utils/assert/assert.ts
diff options
context:
space:
mode:
Diffstat (limited to 'utils/assert/assert.ts')
-rw-r--r--utils/assert/assert.ts10
1 files changed, 10 insertions, 0 deletions
diff --git a/utils/assert/assert.ts b/utils/assert/assert.ts
new file mode 100644
index 0000000..1fd70c8
--- /dev/null
+++ b/utils/assert/assert.ts
@@ -0,0 +1,10 @@
+/**
+ * Throw if a condition is false, narrow the type to truthy otherwise.
+ * For programmer errors (invariants that should never fail); use
+ * Result<T, E> for recoverable failures.
+ */
+export function assert(condition: unknown, message?: string): asserts condition {
+ if (!condition) {
+ throw new Error(message ?? 'Assertion failed')
+ }
+}