strong-typed
v0.4.0
Published
A runtime type-checking library for javascript
Downloads
25
Maintainers
Readme
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:
| Parameter | Type | Description | |-----------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Types | array | A 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. | | Fn | function | The 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 accepteddate
: an instance of the JavaScript Date objectinteger
: a JavaScript number that is an integerbigint
: a JavaScript bigintboolean
: a JavaScript boolean valuedecimal
: a JavaScript number that is not an integerstring
: a JavaScript stringobject
: a JavaScript object, including nullfunction
: a JavaScript functionarray
: a JavaScript arrayset
: a JavaScript Set objectmap
: a JavaScript Map objectnull
: a JavaScript null valueundefined
: 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.