Note that there are some explanatory texts on larger screens.

plurals
  1. POType Checking Array Contents with Closure-Compiler
    primarykey
    data
    text
    <p>In Google Closure, if an <code>Array</code> of a specific <code>@type {Array.&lt;type&gt;}</code> is initialized, can I be sure that Google Closure will confirm the Array contents?</p> <p>Here is a small test case. It appears to me that an <code>{Array.&lt;string&gt;}</code> is sneaking past an <code>{Array.&lt;number&gt;}</code> check, although a <code>{string}</code> is correctly blocked by the same check. I am a little new to GC, is this an error on my part?</p> <p>I've pasted this to the <a href="http://closure-compiler.appspot.com/home" rel="nofollow noreferrer">Google Closure Service</a>, and I'm showing only one of two expected errors (Sept 12 2013). I've double-tested this on my local jar file (newest, v20130823) with <code>ADVANCED_OPTIMIZATIONS</code> and <code>warning_level VERBOSE</code>. It still looks like the <code>{Array.&lt;string&gt;}</code> sneaks by.</p> <p>Docs: <a href="https://developers.google.com/closure/compiler/docs/js-for-compiler" rel="nofollow noreferrer">Annotating for Google Closure</a></p> <p>Thanks in advance for your input.</p> <pre><code>// ==ClosureCompiler== // @output_file_name default.js // @compilation_level ADVANCED_OPTIMIZATIONS // @warning_level VERBOSE // @language ECMASCRIPT5 // ==/ClosureCompiler== /** no warning, as expected: @type {Array.&lt;number&gt;} */ var a = [1,2,3,4]; /** warning! Type mismatch as expected: @type {Array.&lt;number&gt;} */ var b = 'mismatch'; // {string} does not match {Array.&lt;number&gt;} /** no warning, but Type mismatch was expected: @type {Array.&lt;number&gt;} */ var c = ['foo','bar']; // {Array.&lt;string&gt;} should not match {Array.&lt;number&gt;} // prevent compile-to-zero alert(a); alert(b); alert(c); </code></pre> <p><strong>note:</strong> I've taken a close look at <a href="https://stackoverflow.com/questions/7886644/google-closure-compiler-type-annotations-for-array">this related question</a>, where the type of Array.push() was manually filled in. This question concerns initialization, though. If I take his corrected code and init all of his arrays with garbage data, as above, GC fails to catch the garbage in his case as well.</p> <p><strong>Edited:</strong> added <code>warning_level VERBOSE</code> and <code>language ECMASCRIPT5</code> to the header on the test case, just to be sure. <code>{Array.&lt;string&gt;}</code> still not detected.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload