Have ideas to improve npm?Join in the discussion! »

    asrt
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.2 • Public • Published

    asrt

    A small library to verify preconditions and postconditions.

    Usage

    import {always} from 'core';
     
    function add(x, y) {
        always(typeof x === 'number', "Argument 'x' has to be a number.");
        always(typeof y === 'number', "Argument 'y' has to be a number.");
        return x + y;
    }

    always() and assert() functions throw an AssertionError if the condition is false:

    always(1 > 0); // ok
    always(1 < 0); // throws AssertionError

    never() does the same but in reverse:

    never(1 > 0); // throws AssertionError
    never(1 < 0); // ok

    TypeScript

    Asrt functions always() and assert() are typed to assert that the condition you pass them are true, which gives you certainty that your variable is of a given type at runtime.

    export declare function always(condition: boolean, ...messages: ReadonlyArray<string>): asserts condition;
    const x: unknown = someUntypedFunction();
    always(typeof x === 'string');
    const y = x.toUpperCase(); // TypeScript knows that x must be a string, your IDE can suggest toUpperCase() method

    Install

    npm i asrt

    DownloadsWeekly Downloads

    0

    Version

    1.1.2

    License

    MIT

    Unpacked Size

    24 kB

    Total Files

    24

    Last publish

    Collaborators

    • avatar