Assert.falsy()

Syntax

function falsy<T>(value: T, name?: string): void

Asserts that a value is falsy 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: number) {
    Assert.falsy(arg, 'arg');

    // ...
}

doSomething(0);   // OK
doSomething(53);  // AssertionError: "Assertion failed: expected arg to be falsy but was 53"

Remarks

This function is a TypeScript assertion function meaning it behaves exactly the same as if (!value) { ... }, narrowing the type to exclude falsy values if possible.