[英]google closure compiler annotations for passing enums
I can't seem to find the correct closure-compiler annotations to pass a map of enums around. 我似乎找不到正确的闭包编译器注释来传递枚举映射。
Eg: Given EventTargets A
, B
with: 例如:给定EventTargets A
, B
:
/** @enum {string} */
MYNS.A.EventType = {EA : 'ea1'};
/** @enum {string} */
MYNS.B.EventType = {EB : 'eb2'};
I have a constructor C
that returns multiple EventTypes: 我有一个构造函数C
,它返回多个EventType:
/** @return {WHATSTHIS} */
C.prototype.getEventTypesAB = function() {
return {
A: MYNS.A.EventType,
B: MYNS.B.EventType
};
};
to be used later: 稍后使用:
goog.events.listen(parent, c.getEventTypesAB().A.EA, ...);
I've tried a few things such as below, which I guess doesn't work as enum
is not a type
, but am out of ideas of how to annotate this: 我已经尝试了以下一些操作,但由于enum
不是type
,所以我认为它不起作用,但是没有注释方法的想法:
/** @typedef {{ A: MYNS.A.EventType, B: ... }} */
I always end up with the (expected) compiler warning: WARNING - Property EA never defined on String
at the client. 我总是以(预期的)编译器警告结尾: WARNING - Property EA never defined on String
客户端WARNING - Property EA never defined on String
上WARNING - Property EA never defined on String
。
MYNS.A.EventType
is an object whose keys are strings and whose values are members of that enum. MYNS.A.EventType
是一个对象,其键是字符串,其值是该枚举的成员。 So you could write 所以你可以写
/** @type {!Object<string, MYNS.A.EventType>} */ var obj = MYNS.A.EventType
and it would typecheck. 它会进行类型检查。 Likewise for B. So the type of the object literal that you're returning is a record type, {A: !Object<string, MYNS.A.EventType>, B: !Object<string, MYNS.B.EventType>}
同样对于B。因此,您要返回的对象文字的类型是记录类型, {A: !Object<string, MYNS.A.EventType>, B: !Object<string, MYNS.B.EventType>}
This code compiles with the online Closure Compiler Service : 此代码可通过在线Closure编译器服务进行编译 :
goog.provide("MYNS.A.EventType");
goog.provide("MYNS.B.EventType");
goog.provide("MYNS.C.ComboType");
goog.provide("MYNS.C");
/** @enum {string} */
MYNS.A.EventType = {EA : 'ea1'};
/** @enum {string} */
MYNS.B.EventType = {EB : 'eb2'};
/** @typedef {{ A: MYNS.A.EventType, B: MYNS.B.EventType }} */
MYNS.C.ComboType;
MYNS.C = function() {};
/** @return {!MYNS.C.ComboType} */
C.prototype.getEventTypesAB = function() {
return {
A: MYNS.A.EventType,
B: MYNS.B.EventType
};
};
var foo = new C();
var r = foo.getEventTypesAB();
console.log(r.A.EA);
console.log(r.B.EB);
I haven't tried to run the resulting simple-compiled code, but it seems like it should work. 我没有尝试运行生成的简单编译的代码,但似乎应该可以运行。 Here is what the compiler gives with simple-compile and pretty print: 这是编译器提供的简单编译和漂亮的打印结果:
var MYNS = {A:{}, B:{}};
MYNS.A.EventType = {EA:"ea1"};
MYNS.B.EventType = {EB:"eb2"};
MYNS.C = function() {
};
C.prototype.getEventTypesAB = function() {
return {A:MYNS.A.EventType, B:MYNS.B.EventType};
};
var foo = new C, r = foo.getEventTypesAB();
console.log(r.A.EA);
console.log(r.B.EB);
I think the step you are missing is to define the type with a namespace name as in these lines: 我认为您缺少的步骤是使用以下名称空间名称来定义类型:
/** @typedef {{ A: MYNS.A.EventType, B: MYNS.B.EventType }} */
MYNS.C.ComboType;
That is what is shown on this page, which I consider the most helpful page about Google Closure syntax: 这就是该页面上显示的内容,我认为这是有关Google Closure语法最有用的页面:
https://developers.google.com/closure/compiler/docs/js-for-compiler#tags https://developers.google.com/closure/compiler/docs/js-for-compiler#tags
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.