assert
Checks if a condition
is true
. If the condition
is false
, it throws an error
with a specified message. This function is useful for enforcing runtime invariants
and condition
s that must hold. It also uses the TypeScript asserts
keyword to
inform the type checker that the condition
is always true
after the assertion.
- @param
condition
- The condition which should betrue
. - @param
message
- The message to throw if the condition isfalse
. - @param
errorType
- The type of error to throw if the condition isfalse
. - @throws Error If the condition is
false
.
Example
assert(foo !== undefined, 'foo should not be undefined')
assert(typeof bar !== 'string', 'bar should not be of type string')
assert(baz.length > 0', 'baz should have a length greater than 0', RangeError)