From 8e34535d50308bda8f3fe1c58240125ed9f16a54 Mon Sep 17 00:00:00 2001 From: Ian Macartney Date: Thu, 20 Nov 2025 15:11:38 -0800 Subject: [PATCH] allow asserting with a dynamic message --- packages/convex-helpers/index.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/convex-helpers/index.ts b/packages/convex-helpers/index.ts index 4e5f06f0..7a6a5416 100644 --- a/packages/convex-helpers/index.ts +++ b/packages/convex-helpers/index.ts @@ -165,11 +165,15 @@ export type Equals = * assert(x); * // x is now of type string * ``` + * You can also provide a function, to avoid doing expensive string templating. * @param arg A value to assert the truthiness of. - * @param message An optional message to throw if the value is not truthy. + * @param message An optional message to throw if the value is not truthy, or a function to generate the message. */ -export function assert(value: unknown, message?: string): asserts value { +export function assert( + value: unknown, + message?: string | (() => string), +): asserts value { if (!value) { - throw new Error(message); + throw new Error(typeof message === "function" ? message() : message); } }