Miss any of our Open RFC calls?Watch the recordings here! »

@fab1o/type-checking

1.2.0 • Public • Published

@fab1o/type-checking

Throws an Error when data fails to meet params configuration, ensuring data quality, i.e. data is both correct and useful.

npm install @fab1o/type-checking

[Documentation](/Doc.md)

Motivation

  1. Simplification: It makes type-checking clean, declarative, easy to read and maintain;
  2. Performance: Avoids creating the error message before the assertion fails in most cases;
  3. Configurable: Throw your own Error object, and customize the error messages to your liking;
  4. Featureful: Array of types, Optional, Nullable and Undefinable types, Custom types and User-defined types;
  5. Smart error messages enable easier troubleshooting:
    • Dynamic function signature;
    • Base class parameters indicator;
    • Expected type and expected data;
    • Received data;

Example

import { Types, typecheck } from '@fab1o/type-checking';
 
function setName(name, year) {
    const params = {
        name: Types.string,
        year: Types.number.optional
    };
 
    typecheck(setName, params, arguments);
}
 
setName(2020);
// error: setName(name, year) name expected a String but received a Number: 2020
 
setName('SDK', '2020');
// error: setName(name, year) year expected a Number or null or undefined but received a String: "2020"
 
setName('SDK', 2020);
// success

Install

npm i @fab1o/type-checking

DownloadsWeekly Downloads

4

Version

1.2.0

License

MIT

Unpacked Size

126 kB

Total Files

48

Last publish

Collaborators

  • avatar