[英]How to annotate nested object in javascript for Closure Compiler and have all properties optional?
Here is my class A and I want all options to be optional. 这是我的A级,我希望所有选项都是可选的。 It works fine for a,b,c properties but it doesn't work for c.cX properties. 它适用于a,b,c属性,但它不适用于c.cX属性。 How to do it properly 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() {}
}
});
Options
type should be defined like this: 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.