assert
The classic assert function, it takes a value as input, if the value is falsy it throws or else it does nothing. Functionally it can be summed up to this:
Typewise however, it takes advantage of the asserts condition statement. If you pass a type guard as value TypeScript can make inference on what happens after the assert
instruction.
Assertion on types
Assert can also be used to confirm assertion on types.
You can for example test if a type extends another by doing:
You can also test the equality of two types with:
Equalsassert + is
isError thrown
When the value is falsy assert throws an instance of AssertionError
. Assertion error, extends Error and can be imported like this:
A specific error message can be passed as second argument to the assert function.
Last updated