introspect-typed
Provides rudimentary type checking and function overloading in Javascript.
- Install
- API
Install
npm install introspect-typed
API
matchType
: Type checking individual values
Other methods (typeChecked
, overload
) are based on this functionality.
Direct match
var matchType = matchType; ; // => true ; // => false
Getting a matcher function
var matchType = matchType; var stringMatcher = ; ; // => true ; // => false
Custom types work as well
var matchType = matchType; { thistype = 'custo'; } var custoMatcher = ; ; // => true ; // => false
Matching against any type
Use Typed.Any
if you want to match any value.
var Typed = ; var matchType = TypedmatchType; ; // => true
Matching against several types
Use Typed.Either
if you want to match several types.
var Typed = ; var matchType = TypedmatchType; ; // => true ; // => true ; // => false
Matching against a predicate
Use Typed.Matcher
if you want to match with a predicate.
var Typed = ; var matchType = TypedmatchType; var type = Typed; ; // => true ; // => true ; // => true ; // => false ; // => false
Matching against an ES6 iterable
Iterables are objects with a Symbol.iterator
property containing a function.
In ES6, strings, arrays, sets, maps and generator functions, among others, are
iterables.
Use Typed.Iterable
if you want to match es6 iterables.
var Typed = ; var Iterable = TypedIterable; ; // => true ; // => true ; // => true ; // => false ; // => false
typeChecked
: Type checking function calls
var Typed = ; var typeChecked = TypedtypeChecked; var Either = TypedEither; { thistype = 'custo'; }
Declare a type checked function by calling typeChecked
with
- an array of types,
- the function you want to type check.
var tcFn = ; ; // => 'string[object Object]' ; // => 'Infinity[object Object]'
Rest parameters
Check type for rest parameters by using Typed.Rest
. In ES6:
var Rest = TypedRest;var typedFn =
In ES5:
var Rest = TypedRest; var typedFn = ;
Rest(String)
will match all strings at the end of the arguments.Rest(Any)
will match anything at the end of the arguments.Rest(Either(X,Y))
will match any number ofX
s andY
s at the end of the arguments.
Note that Rest(...)
must be the last element in the type array, otherwise
an error will be thrown while matching.
Error management
By default, the typechecked function throws a TypeError
when not given
proper arguments.
; // => throws TypeError
You can set the error management behaviour yourself (at any time).
tcFn;
And set back the original throwing behaviour by calling onError
with
a falsey value.
tcFn;
overload
: Overloading functions
Chaining methods
When creating an API, we often want our methods to accept different kinds of inputs, but the code that results from these checks is ugly and provides many occasions for bugs.
Instead, using overload
, one can do the following:
var overload = overload; var createObj = ;
Without the comments:
var overload = overload; var createObj = ;
Calling a named overloaded method directly bypasses the type check.
To have a completely type checked method, one could do this instead:
var overload = overload; var typeChecked = typeChecked; var createObj = ;
The calls to overload
with arguments matching [Array, Function]
are
equivalent to overload(typeChecked(types, fn))
.
Complete overloading
var overload = overload; var fn = // give a number : increments it // give two numbers : multiply them // give two strings: join them with '-' // give 3 strings: join them with '!' // returns the length for a String or an Array // logs arguments when there are 4 of them // give a number and a string: repeat the string n times ;
Calling this method with different kinds of arguments changes its behaviour.
tobe; tobe; tobe; tobe; tobe;
The this
value is conserved.
tobe;
build
The build
function allows to create a new Typed context.
This allows to modify the matchType
function, to pass more cases to the
type checking.
var build = build; var newTypedContext = ; var matchType = newTypedContextmatchType; var Truthy = {}; tobefalse; tobefalse; matchType; tobetrue; tobefalse;