Skip to content

Commit 22df7f7

Browse files
authored
allow asserting with a dynamic message (#862)
<!-- Describe your PR here. --> <!-- The following applies to third-party contributors. Convex employees and contractors can delete or ignore. --> ---- By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
2 parents 650d0ee + 8e34535 commit 22df7f7

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

packages/convex-helpers/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,15 @@ export type Equals<X, Y> =
165165
* assert(x);
166166
* // x is now of type string
167167
* ```
168+
* You can also provide a function, to avoid doing expensive string templating.
168169
* @param arg A value to assert the truthiness of.
169-
* @param message An optional message to throw if the value is not truthy.
170+
* @param message An optional message to throw if the value is not truthy, or a function to generate the message.
170171
*/
171-
export function assert(value: unknown, message?: string): asserts value {
172+
export function assert(
173+
value: unknown,
174+
message?: string | (() => string),
175+
): asserts value {
172176
if (!value) {
173-
throw new Error(message);
177+
throw new Error(typeof message === "function" ? message() : message);
174178
}
175179
}

0 commit comments

Comments
 (0)