How to annotate a nested object in javascript for the Closure Compiler and have all the properties optional?

Here is my class A, and I want all options to be optional. It works fine for properties a, b, c, but does not work for c.cX properties. How to do it right to make all properties optional?

// ==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() {}
    }
});
+4
source share
1 answer

Options type must be defined as follows:

/**
 * @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;
+2
source

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


All Articles