Re: [closure-compiler-discuss] Getting a "Bad Type Annotation" with my namespace - suggestions?

135 views
Skip to first unread message

John Lenz

unread,
Apr 8, 2013, 5:54:11 PM4/8/13
to closure-compiler
This works:

/** @const */ var ns1 = {};
/** @const */ ns1.ns2 = {};

(function (ns) {

   /** @constructor */
   ns.myClass = function () {
      this.sampleProp = 'Hi there';
   }

   ns.myClass.prototype = {
      /** @return {ns.myClass} */
      getMe : function () {
         return this;
      }
   }

})(ns1.ns2);

/** @const */ var Mine = ns1.ns2;

var myInstance = new Mine.myClass();
myInstance.getMe();




On Mon, Apr 8, 2013 at 2:49 PM, Kelly Morrison <kelly.m...@performancematters.com> wrote:
I've been encountering the "Bad type annotation" when trying to use a custom namespace. I've distilled the problem down into a simple example that fails on the online closure compiler when using Advanced mode.

Is there something obvious I'm missing here? Thanks in advance for any suggestions.

Here's the error:

JSC_TYPE_PARSE_ERROR: Bad type annotation. Unknown type ns.myClass at line 15 character 13
/** @return {ns.myClass} */
^
And here's the test case I ran through the online Closure Compiler in Advanced mode:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @formatting pretty_print
// ==/ClosureCompiler==

// ADD YOUR CODE HERE
var ns1 = {};
ns1.ns2 = {};

var myNameSpace = ns1.ns2;

(function (ns) {

   /** @constructor */
   ns.myClass = function () {
      this.sampleProp = 'Hi there';
   }

   ns.myClass.prototype = {
      /** @return {ns.myClass} */
      getMe : function () {
         return this;
      }
   }

})(myNameSpace);

var myInstance = new myNameSpace.myClass();
myInstance.getMe();

--
 
---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-d...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Dimitris Vardoulakis

unread,
Apr 8, 2013, 5:54:55 PM4/8/13
to closure-comp...@googlegroups.com, Chad Killingsworth
Chad, is there a chance that this is the same issue as 

Chad Killingsworth

unread,
Apr 8, 2013, 6:03:43 PM4/8/13
to closure-comp...@googlegroups.com, Chad Killingsworth
Yes - but in my case @const doesn't work. If the root namespace is also a constructor, you can't work around the issue.

/**
 * @constructor
 * @const
 */
function foo() {}

(function() {
  /** @constructor */
  foo.bar = function() {};
})();

/** @param {foo.bar} x */
function foobar(x) {}

This is one of those annoying parts of the compiler that comes up regularly and the solution is pretty obscure if it exists.

thanpolas

unread,
Apr 9, 2013, 3:41:28 AM4/9/13
to closure-comp...@googlegroups.com
Kelly why don't you use closure's native namespace function? (goog.provide)

On Tuesday, April 9, 2013 1:06:09 AM UTC+3, Kelly Morrison wrote:
Thanks, John. I was afraid the answer would look like that: in the "real world" code I use a more complicated method of creating the namespace (as listed in Test-Driven JavaScript Development by Christian Johansen) than the stripped-down version I used in the example. Here's the actual namespace generation. I guess I'll have to update my code to build up the namespace instead.

/** @const */ var myNameSpace = (function () {
function namespace(string) {
var object = this;
var levels = string.split('.');
for (var i = 0, l = levels.length; i < l; i++) {
if (typeof object[levels[i]] === 'undefined') {
object[levels[i]] = {};
}
object = object[levels[i]];
}
return object;
}
return { namespace: namespace };
}());
Reply all
Reply to author
Forward
0 new messages