InvalidType

Thrown to indicate that the type of a value isn't expected.

Source:
InvalidType.js, line 40
Examples
function greet(name) {
  var checked = InvalidType.check(name, 'string');
  alert('Hello, ' + checked);
}
greet('John'); // "Hello, John"
greet(1234);   // Throws: "unexpected type of value ..."
Basic Usage
var value = 'ABC';
InvalidType.check(value, 'boolean', 'string', 'object'); // OK
Multiple Expectations
class Beagle extends Dog {
  ...
}
var oliver = new Beagle();
InvalidType.check(oliver, Dog);      // OK
InvalidType.check(oliver, Beagle);   // OK
InvalidType.check(oliver, ShibaInu); // Throws
Class Checking
value = 123;
try {
  InvalidType.check(value, 'string', 'object');
} catch (e) {
  console.debug( e.info.checked  ); // 123
  console.debug( e.info.expected ); // ['string', 'object']
  console.debug( e.info.actual   ); // 'number'
}
Using .info for debug inside 'catch'

Extends

Exception

Members

readonlyinfo any

Additional informations for debug.

Inherited From:
Exception#info
Source:
Exception.js, line 23

Methods

staticcheck(value, …expected)any

Checks if the type of value matches with expected. If it does, just returns value.
Otherwise, triggers an exception.

The triggered exception holds value and expected as .info.checked and .info.expected.
And the actual type is stored in .info.actual.

Parameters:
Name Type Attributes Description
value any

A value to check the type

expected string | class <repeatable>

The expected type(s)

Available Types
Type Description
Any type that typeof returns See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
bool Alias of boolean
int, integer Matches with integer numbers
iterable Matches with an array or array-like object
A class (constructor function) Matches with an instance of the class
Source:
InvalidType.js, line 77
Returns:

Just returns the value argument if there's no problem

Type:
any

expects(type)boolean

Returns if this exception expected type.

Parameters:
Name Type Description
type string | class
Overrides:
Exception#expects
Source:
InvalidType.js, line 47
See:
InvalidType.check
Returns:
Type:
boolean

trigger()any

Throws this exception if handler option is not set.
If handler option is set, calls it and returns the result.

Inherited From:
Exception#trigger
Source:
Exception.js, line 41
See:
Exception.option
Throws:

This exception

Returns:

The result of option.handler function

Type:
any

Documentation generated by JSDoc 3.6.6
on
using docolatte theme