如何为 Closure 编译器注释 javascript 中的嵌套对象并使所有属性都是可选的?
How to annotate nested object in javascript for Closure Compiler and have all properties optional?
这是我的 class A,我希望所有选项都是可选的。它适用于 a、b、c 属性,但不适用于 c.cX 属性。如何正确地使所有属性成为可选的?
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==
/**
* @typedef {{
* a: (string|undefined),
* b: (number|undefined),
* c: ({
* ca: (string|undefined),
* cb: (number|undefined),
* cc: (Function|undefined)
* }|undefined)
* }}
*/
var Options;
/**
* @param {Options=} options
* @constructor
*/
var A = function(options) {
console.log(this);
};
new A({
a: 'x',
c: {
ca: 'x',
//cb: 1,
cc: function() {}
}
});
Options
类型应该这样定义:
/**
* @record
* @struct
*/
function Options() {};
/** @type {string|undefined} */
Options.prototype.a;
/** @type {number|undefined} */
Options.prototype.b;
/** @type {!OptionsC|undefined} */
Options.prototype.c;
/**
* @record
* @struct
*/
function OptionsC() {};
/** @type {string|undefined} */
OptionsC.prototype.ca;
/** @type {number|undefined} */
OptionsC.prototype.cb;
/** @type {Function|undefined} */
OptionsC.prototype.cc;
这是我的 class A,我希望所有选项都是可选的。它适用于 a、b、c 属性,但不适用于 c.cX 属性。如何正确地使所有属性成为可选的?
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==
/**
* @typedef {{
* a: (string|undefined),
* b: (number|undefined),
* c: ({
* ca: (string|undefined),
* cb: (number|undefined),
* cc: (Function|undefined)
* }|undefined)
* }}
*/
var Options;
/**
* @param {Options=} options
* @constructor
*/
var A = function(options) {
console.log(this);
};
new A({
a: 'x',
c: {
ca: 'x',
//cb: 1,
cc: function() {}
}
});
Options
类型应该这样定义:
/**
* @record
* @struct
*/
function Options() {};
/** @type {string|undefined} */
Options.prototype.a;
/** @type {number|undefined} */
Options.prototype.b;
/** @type {!OptionsC|undefined} */
Options.prototype.c;
/**
* @record
* @struct
*/
function OptionsC() {};
/** @type {string|undefined} */
OptionsC.prototype.ca;
/** @type {number|undefined} */
OptionsC.prototype.cb;
/** @type {Function|undefined} */
OptionsC.prototype.cc;