From 5545d160572d8a27f56d61321e4358ac861fe807 Mon Sep 17 00:00:00 2001 From: Mattias Andersson Date: Wed, 4 Sep 2024 13:36:17 +0200 Subject: [PATCH] Add invariant method (#780) - Add invariant method to core. --- packages/core/src/index.ts | 1 + packages/core/src/utils/Invariant.ts | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 packages/core/src/utils/Invariant.ts diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 985266eb7..2165e9be0 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -52,3 +52,4 @@ export * from "./utils/TruthyKeysAsList"; export * from "./utils/PropsForwarder"; export * from "./utils/parsers/NumberParser"; export { booleanOrNumberToNumber } from "./utils/BooleanOrNumberToNumber"; +export * from "./utils/Invariant"; diff --git a/packages/core/src/utils/Invariant.ts b/packages/core/src/utils/Invariant.ts new file mode 100644 index 000000000..d53caf37e --- /dev/null +++ b/packages/core/src/utils/Invariant.ts @@ -0,0 +1,8 @@ +export function invariant( + val: T | null | undefined, + message: string +): asserts val is T { + if (val == null) { + throw new Error("Invariant error: " + message); + } +}