1.1.2 • Published 4 years ago
asrt v1.1.2
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
1.1.1
4 years ago
1.1.2
4 years ago
1.1.0
4 years ago
1.0.5-beta.3
4 years ago
1.0.5-beta.1
4 years ago
1.0.5-beta.2
4 years ago
1.0.5
4 years ago
1.0.4
4 years ago
1.0.3
4 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago