I’m getting a lot of "Unknown type" warnings when running a fairly large library through Closure Compiler, and they seem to occur when my types are declared in self-executing anonymous functions. There’s nothing exotic about this, but if I strip the self-executing functions out, the type declarations seem to work (at least in this simple test).
I’m not sure if there’s something wrong with my code annotations or if there’s anything illegal in the code, but I think this is all kosher and the standard way to modularize an API.
The following test code creates a namespace (just a plain old JS object) and attaches an enum (an object literal) and a function to it.
var mynamespace = {};
(function (mynamespace) {
/**
* Some enum.
* @enum {number}
*/
mynamespace.SomeEnum = {
FOO: 1,
BAR: 2
};
/**
* Frazzle some type.
* @param {mynamespace.SomeEnum} qux The type to frazzle.
* @return {boolean} whether the operation succeeded.
*/
mynamespace.frazzle = function(qux) {
return true;
}
}(mynamespace));
// call it
mynamespace.frazzle(mynamespace.SomeEnum.FOO);
Looks fine, right? The closure compiler errors:
[jscomp] Compiling 1 file(s) with 37 extern(s)
[jscomp] X:\dev\solclientjs\sdk\tools\jscomptest.js:14: WARNING - Parse error. Unknown type mynamespace.SomeEnum
[jscomp] * @param {mynamespace.SomeEnum} qux The type to frazzle.
Edit:
Original answer was totally off.
This definitely appears to be a bug in the compiler. I haven’t found a bug report with this exact issue, but I found two bug reports that appear to address the inverse of this issue (compiler should be throwing a warning, but it won’t unless you unwrap the anonymous function).
http://code.google.com/p/closure-compiler/issues/detail?id=134
http://code.google.com/p/closure-compiler/issues/detail?id=61
In any case it looks like anonymous functions are wonky when used with type expressions.