Assert.truthy()
Syntax
function truthy<T>(value: T, name?: string): void
Asserts that a value is truthy and throws an assertion error if it isn’t.
Parameters | |
value
|
The value to check. |
name
Optional
|
A variable, property, or parameter name which, if specified, will print the name as part of the assertion error to make it more clear where the error originated. |
Example
import { Assert } from 'potence';
function doSomething(arg: string) {
Assert.truthy(arg, 'arg');
// ...
}
doSomething('hello'); // OK
doSomething(''); // AssertionError: 'Assertion failed: expected arg to be truthy but was ""'
Remarks
This function is a
TypeScript assertion function
meaning it behaves exactly the same as if (value) { ... }
,
narrowing the type to exclude null
, undefined
, and false
,
among others.