I’m trying out Google Closure, specifically the annotating stuff to enforce type safety. To test I did something wrong, though the compiler won’t tell me that it is…
Here’s the code:
// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level SIMPLE_OPTIMIZATIONS
// ==/ClosureCompiler==
/**
* A card.
* @constructor
* @param {String} cardName The exact name of the card
* @param {Kinetic.Layer} layer The layer for the card
*/
function CardObject(cardName, layer)
{
/** @type {Number} */
var number = cardName;
}
So, I have a variable number which I say is a Number, and I try to assign a string to it. This shouldn’t be possible, right? Though the compiler won’t tell me that…
Why won’t it tell me that’s wrong?
Closure Compiler uses warning levels to determine which checks are enabled during the compilation process. The three warning levels are:
For example, using compilation level
SIMPLE_OPTIMIZATIONS, you will still get type-check warnings with the warning level set toVERBOSE.Output
To understand exactly which checks are associated with each warning level, here is the relevant code from WarningLevels.java.
QUIET
DEFAULT
VERBOSE
Notice that
options.checkTypes = true;is only set for the VERBOSE warning level. As Speransky Danil pointed out, type checking is also enabled when using compilation levelADVANCED_OPTIMIZATIONS.In addition, classes of warnings may be controlled individually with the Closure Compiler application (jar file) using compiler flags:
The warning classes that may be specified are as follows:
For example, type checking warnings could be enabled individually: