JSPM

  • Created
  • Published
  • Downloads 42
  • Score
    100M100P100Q81251F
  • License MIT

TypeScript assertion helpers

Package Exports

  • assertate

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (assertate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Assertate

TypeScript 3.7 assertion helper library

Requirements

Requirement Version
TypeScript >=3.7.0

About

A minimal library exposing basic TypeScript 3.7 assertions helpers.

Installation

yarn add assertate

Example

import {
  assert,
  assertIsNumber,
  getAssertionMessage,
  getType,
  isNumber,
  isString,
  setAssertionMessage
} from "assertate";

////////////////////////////////////////////////////////////////////////////////
// use the `assertIs` functions for exception based assertions
////////////////////////////////////////////////////////////////////////////////
try {
  const someUndefinedVar: unknown = undefined;
  assertIsNumber(someUndefinedVar); // will throw an Error
} catch (err) {
  console.error(err); // An assertion Error will be logged with the message set earlier by `setAssertionMessage`
}

const someNumericVar: unknown = 123.456;
assertIsNumber(someNumericVar); // will not throw
const asFixedPointZero = someNumericVar.toFixed(0); // compiler now knows `someNumericVar` is a number and has all instance methods that a number has

////////////////////////////////////////////////////////////////////////////////
// use the `is` functions for control-flow based assertions
////////////////////////////////////////////////////////////////////////////////
const anotherNumber: unknown = 123.123;
if (isNumber(anotherNumber)) {
  // compiler now knows that in this if block, anotherNumber` is a number
  const anotherNumberAsFixedPointZero = anotherNumber.toFixed(0);
}

////////////////////////////////////////////////////////////////////////////////
// General assertion
// - use the `assert` function if you need to compose more custom assertions
////////////////////////////////////////////////////////////////////////////////
const someNumberOrString: unknown = "123";
assert(isNumber(someNumberOrString) || isString(someNumberOrString)); // compiler now knows someNumberOrString is of type `number | string`

////////////////////////////////////////////////////////////////////////////////
// Set your own assertion messages
// - a default assertion message is provided; will most likely be good enough for most cases
////////////////////////////////////////////////////////////////////////////////
const defaultAssertionMessage = getAssertionMessage();
setAssertionMessage(
  (someValue, expectedType) =>
    `Expected a ${expectedType}, got a ${getType(someValue)}`
);
try {
  const aNumber: unknown = 123;
  assertIsString(aNumber);
} catch (err) {
  console.log(err); // An error with message 'Expected a string, got a number' will be logged
}

API

Swing by the docs to get a full look at the available functions.