An awesome library for type checking on the fly.
- watchObj(obj, types) ⇒
Object
Watches an object, and when it changes it checks types.
- watchArr(arr, types) ⇒
Array
Watches an array, and when it changes it checks types.
- watchFunc(func, types) ⇒
function
Watches an function, and when it gets called it checks argument types.
- type :
String
|Array.<type>
A type. A type can be a string for checking against one type, an array of types for checking against multiple types, or falsy for no type checking. A type string is a value supported by type-detect.
Watches an object, and when it changes it checks types.
Kind: global function
Returns: Object
- A copy of the object. It watches this value, if you edit the object you passed into
the function, nothing will happen.
Param | Type | Description |
---|---|---|
obj | Object |
The object to watch. |
types | Object.<*, type> |
An object where the key is the property name, and the value is a type. |
Example
// Watches an empty object.
const obj = types.watchObj({}, { name: 'string', age: 'number' });
obj.name = 'foo'; // all good
obj.age = 'bar'; // throws error
obj.height = '12'; // ignored
Example
// Watches a non-empty object.
const obj = types.watchObj({
name: 'foo', // all good
age: 'bar', // throws error
height: '12', // ignored
}, { name: 'string', age: 'number' });
Watches an array, and when it changes it checks types.
Kind: global function
Returns: Array
- A copy of the array. It watches this value, if you edit the array you passed into
the function, nothing will happen.
Param | Type | Description |
---|---|---|
arr | Array |
The array to watch. |
types | Array.<type> |
An array of types, where the type's index corresponds to the index of the value in the array. |
Example
// Watches an empty array.
const arr = types.watchArr([], ['string', 'number']);
arr.push('foo'); // all good
arr.push('bar'); // throws error
arr.push('12'); // ignored
Example
// Watches a non-empty array.
const arr = types.watchArr([
'foo', // all good
'bar', // throws error
'12', // ignored
], ['string', 'number']);
Watches an function, and when it gets called it checks argument types.
Kind: global function
Returns: function
- A copy of the function. It watches this value, if you edit the function you passed into
the function, nothing will happen.
Param | Type | Description |
---|---|---|
func | function |
The function to watch. |
types | Array.<type> |
An array of types, where the type's index corresponds to the index of the argument. |
Example
// Watches a function.
const func = types.watchFunc((name, age, height) => {
console.log(name, age, height);
}, ['string', 'number']);
func(
'foo', // all good
'bar', // throws error
'12' // ignored
);
type : String
| Array.<type>
A type. A type can be a string for checking against one type, an array of types for checking against multiple types, or falsy for no type checking. A type string is a value supported by type-detect.
Kind: global typedef