require('type-check'); returns an object that exposes four properties. VERSION is the current version of the library as a string. typeCheck, parseType, and parsedTypeCheck are functions.
typeCheck checks a JavaScript value input against type written in the type format (and taking account the optional options) and returns whether the input matches the type.
arguments
type - String - the type written in the type format which to check against
input - * - any JavaScript value, which is to be checked against the type
options - Maybe Object - an optional parameter specifying additional options, currently the only available option is specifying custom types
returns
Boolean - whether the input matches the type
example
typeCheck('Number', 2); // true
parseType(type)
parseType parses string type written in the type format into an object representing the parsed type.
arguments
type - String - the type written in the type format which to parse
returns
Object - an object in the parsed type format representing the parsed type
example
parseType('Number'); // [{type: 'Number'}]
parsedTypeCheck(parsedType, input, options)
parsedTypeCheck checks a JavaScript value input against parsed type in the parsed type format (and taking account the optional options) and returns whether the input matches the type. Use this in conjunction with parseType if you are going to use a type more than once.
arguments
type - Object - the type in the parsed type format which to check against
input - * - any JavaScript value, which is to be checked against the type
options - Maybe Object - an optional parameter specifying additional options, currently the only available option is specifying custom types
A basic type, eg. Number, uses this check. This is much more versatile than using typeof - for example, with document, typeof produces 'object' which isn't that useful, and our technique produces 'HTMLDocument'.
You may check for multiple types by separating types with a |. The checker proceeds from left to right, and passes if the value is any of the types - eg. String | Boolean first checks if the value is a string, and then if it is a boolean. If it is none of those, then it returns false.
Adding a Maybe in front of a list of multiple types is the same as also checking for Null and Undefined - eg. Maybe String is equivalent to Undefined | Null | String.
You may add a comment to remind you of what the type is for by following an identifier with a :: before a type (or multiple types). The comment is simply thrown out.
The wildcard * matches all types.
There are three types of structures for checking the contents of a value: 'fields', 'tuple', and 'array'.
If used by itself, a 'fields' structure will pass with any type of object as long as it is an instance of Object and the properties pass - this allows for duck typing - eg. {x: Boolean}.
To check if the properties pass, and the value is of a certain type, you can specify the type - eg. Error{message: String}.
If you want to make a field optional, you can simply use Maybe - eg. {x: Boolean, y: Maybe String} will still pass if y is undefined (or null).
If you don't care if the value has properties beyond what you have specified, you can use the 'etc' operator ... - eg. {x: Boolean, ...} will match an object with an x property that is a boolean, and with zero or more other properties.
For an array, you must specify one or more types (separated by |) - it will pass for something of any length as long as each element passes the types provided - eg. [Number], [Number | String].
A tuple checks for a fixed number of elements, each of a potentially different type. Each element is separated by a comma - eg. (String, Number).
An array and tuple structure check that the value is of type Array by default, but if another type is specified, they will check for that instead - eg. Int32Array[Number]. You can use the wildcard * to search for any type at all.