如何在javascript中为Closure Compiler注释嵌套对象,并且所有属性都是可选的?

这是我的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() {}
    }
});

最佳答案 选项类型应该像这样定义:

/**
 * @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;
点赞