0.4.0 • Published 1 year ago

strong-typed v0.4.0

Weekly downloads
1
License
MIT
Repository
github
Last release
1 year ago

Test install size License: MIT

strong-typed

strong-typed is a lightweight and easy-to-use library that can help you catch type errors at runtime and improve the reliability of your code. It is compatible with modern JavaScript features, supports a wide range of types, and provides clear error messages.

Installation

npm i strong-typed

Usage

Strong-typed takes two arguments:

ParameterTypeDescription
TypesarrayA javascript array of supported types (supported types are any, date, integer, boolean, decimal, string, object, function, array, set, map, null, undefined). The number of elements in the array must match the parameter number of function that's passed in.
FnfunctionThe javascript function to be type checked

Examples

import strongTyped, {Types} from 'strong-typed';

const myFunc = st([Types.STRING], (a) => { console.log(a) });

myFunc();    // Error: Function expects 1 arguments, instead only 0 parameter(s) passed in.
myFunc(1);   // Error: Expected argument 1 to be of type 'string'
myFunc("1"); // Will print 1 in the console
import strongTyped, {Types} from 'strong-typed';

const myFunc = strongTyped([Types.STRING, Types.INTEGER], (a, b) => { console.log(a + b) });

myFunc("1", 2);  // Will print 3 in the console
myFunc("1", "2");  // Error: Expected argument 2 to be of type 'integer'
import strongTyped, {Types} from 'strong-typed';

class MyClass{
    constructor(a,b){
        this.a = a;
        this.b = b;
    }
}
const myFunc = strongTyped([MyClass, Types.INTEGER], (a, b) => { console.log(a.a + b) });

myFunc(new MyClass(1,2), 2);  // Will print 3 in the console
myFunc({a:1, b:2}, 2);  // Error: Expected argument 1 to be of type 'MyClass'

Browser Usage

To use strong-typed in a browser environment, you can include the library in a script tag on your HTML page. Make sure to include the library after you include any dependencies:

<script src="path/to/strong-typed.js"></script>
<script>
  const myFunc = strongTyped([Types.STRING], (a) => { console.log(a) });
  myFunc("1"); // Will print 1 in the console
</script>

Supported Types

The following types are supported by the library:

  • any: any type of value is accepted
  • date: an instance of the JavaScript Date object
  • integer: a JavaScript number that is an integer
  • bigint: a JavaScript bigint
  • boolean: a JavaScript boolean value
  • decimal: a JavaScript number that is not an integer
  • string: a JavaScript string
  • object: a JavaScript object, including null
  • function: a JavaScript function
  • array: a JavaScript array
  • set: a JavaScript Set object
  • map: a JavaScript Map object
  • null: a JavaScript null value
  • undefined: a JavaScript undefined value

When defining the types for your function, make sure the number of elements in the Types array matches the number of arguments in your function. If a type is not recognized, the library will throw an error and the function will not execute.

Error handling

When an error occurs, the library will throw an error with a descriptive message indicating which argument caused the error and what type it was expected to be. For example, if the first argument passed to a function was expected to be a string but was passed as a number, the error message would be: Expected argument 1 to be of type 'string'. Please note that if the function passed to the library is an anonymous function, it will not show any name in the error message.