goog.asserts
Module ID |
---|
Utilities to check the preconditions, postconditions and invariants runtime.
Methods in this package are given special treatment by the compiler
for type-inference. For example, goog.asserts.assert(foo)
will make the compiler treat foo
as non-nullable. Similarly,
goog.asserts.assertNumber(foo)
informs the compiler about the
type of foo
. Where applicable, such assertions are preferable to
casts by jsdoc with @type
.
The compiler has an option to disable asserts. So code like:
var x = goog.asserts.assert(foo());
goog.asserts.assert(bar());
will be transformed into:
var x = foo();
The compiler will leave in foo() (because its return value is used),
but it will remove bar() because it assumes it does not have side-effects.
Additionally, note the compiler will consider the type to be "tightened" for
all statements after the assertion. For example:
const /** ?Object &#ast;/ value = foo();
goog.asserts.assert(value);
// "value" is of type {!Object} at this point.