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

1.0.0 • Public • Published

assert-tsn

assertions for typescript and javascript

Installation

npm install assert-tsn --save

Usage

Typescript or JavaScript

All these functions throw exceptions if a false assertion is encountered. In addition if console.error is defined and the Error class supports stack traces, a stack trace will be printed to the console. And if the alert function exists in the global context, then it is called with the message.

The check function:

import assert = require( 'assert' ) ;

assert.check( 2+2===4, "Message zero") ;
assert.check( 2+2===5, "Message one" ) ;

Should throw an exception new Error("Assertion failed: Message one"). The message is optional.

The checkEqual function:

import assert = require( 'assert' ) ;

assert.checkEqual( expected, actual ) ;

Uses === for comparison.

The checkInvariant function:

import assert = require( 'assert' ) ;

...

class Foo {

    private ok() : boolean { ... }
    
    public foo() : void {
        ...
        assert.checkInvariant( ok(), "Corrupted Foo" ) ;
    }
}

Message is optional.

The checkPrecondition function.

import assert = require( 'assert' ) ;

function someFunction( i : number, n : number) : string {
    assert.checkPrecondition( 0 <= i && i < n ) ;
}

The message is optional. The result type is never.

check, checkPrecondition, and checkInvariant are essentially the same, but differ in their intended usage and in their messages.

The failedPrecondition function.

import assert = require( 'assert' ) ;

function someFunction( i : number) : string {
    switch( i ) {
        case -1: return "too small" ;
        case 0 : return "just right" ;
        case +1 : return "too big" ;
        default: return assert.failedPrecondition("out of range") ;
}

The message is optional. The result type is never. Since never is a subtype of all types, the "value" of failedPrecondition can be returned from any function.

The todo function

import assert = require( 'assert' ) ;

function someFunctionWithAResult() : string {
    return assert.todo( "someFunctionWithAResult not yet implemented" ) ; 
}

function someVoidFunction() : void {
    assert.todo( "someVoidFunction not yet implemented" ) ; 
}

The message is optional. The result type is never. Since never is a subtype of all types, the "value" of todo can be returned from any function.

The unreachable function.

import assert = require( 'assert' ) ;

...
    let b : boolean ;
    switch( s ) {
        case "yes": b = true  ;
        case "no" : b = false ;
        default: return assert.unreachable("s has a bad value") ;
}

The message is optional. The result type is never. Since never is a subtype of all types, the "value" of unreachable can be returned from any function.

unreachable, todo and failedPrecondition are essentially all the same but differ in their intended usage and the error messages differ.

Package Sidebar

Install

npm i assert-tsn

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

19.7 kB

Total Files

13

Last publish

Collaborators

  • theodore.norvell