问题
I have something like this:
let { total } = settings;
How do I document the total variable? I tried something like this:
/**
* @type {Object}
* @property {String} total.test
*/
let { total } = settings;
but it doesn't seem to be the right way.
Any suggestions?
回答1:
@Tommy-Pepsi Gaudreau was so close in his comment on the original question.
Here's an example in the closure compiler tool @ closure-compiler.appspot.com
let /** @type {Object<string|boolean>} */ settings = {};
let str = 'string';
let bool = true;
settings.b = bool;
settings.s = str;
// Note that at this point, b and s are of the type {string|boolean}.
let {/** @type {string} */ s,/** @type {boolean} */ b } = settings;
console.log({b, s});
// But now, when we assign the wrong types, we get a warning.
b='warn';
s=false;
Number of warnings: 2
JSC_TYPE_MISMATCH: assignment
found : string
required: boolean at line 15 character 4
b='warn';
^
JSC_TYPE_MISMATCH: assignment
found : boolean
required: string at line 16 character 4
s=false;
^
Edit - Sep 27, 2018: I've reduced the amount of initial typing to ensure/clarify the types weren't being ignored, and that the warnings were coming from the types in the destructuring.
来源:https://stackoverflow.com/questions/52544065/how-to-document-destructured-variable-with-jsdoc