Is there a way to use an enumeration as a type in the Google Closure Compiler?

I tried to do something like @param {window.MyNamespace.MyEnum} myVar , but the compiler complained about JSC_TYPE_PARSE_ERROR: Bad type annotation. Unknown type window.MyNamespace.MyEnum JSC_TYPE_PARSE_ERROR: Bad type annotation. Unknown type window.MyNamespace.MyEnum .

Should I make @typedef an enumeration or just use @param {number} if my enum is @enum {number} ? I really prefer the enumeration, as other values ​​are really not allowed.

 (function (MyNamespace) { /** * @enum {number} */ MyNamespace.MyEnum = { FOO: 1, BAR: 2, BAZ: 3 } /** * @constructor * @param {Object} foo */ MyNamespace.MyClass = function (foo) { this.foo = foo } /** * @constructor * @param {MyNamespace.MyClass} bar */ MyNamespace.MyOtherClass = function (bar) { this.bar = bar } /** * @param {MyNamespace.MyEnum} baz */ MyNamespace.MyOtherClass.prototype.someMethod = function (baz) { } })(window.MyNamespace = window.MyNamespace || {}) 
+4
source share
1 answer

Alias ​​types using a function parameter are poorly supported in the Closure compiler. Use the --output_wrapper flag to include code after compilation. The following code compiles correctly:

 /** @const */ var MyNamespace = window.MyNamespace || {}; /** @enum {number} */ MyNamespace.MyEnum = { FOO: 1, BAR: 2, BAZ: 3 }; /** * @constructor * @param {Object} foo */ MyNamespace.MyClass = function (foo) { this.foo = foo }; /** * @constructor * @param {MyNamespace.MyClass} bar */ MyNamespace.MyOtherClass = function (bar) { this.bar = bar }; /** @param {MyNamespace.MyEnum} baz */ MyNamespace.MyOtherClass.prototype.someMethod = function (baz) {}; 
+4
source

Source: https://habr.com/ru/post/1496558/


All Articles