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?
@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.
for any direct destructured variable you can try this workaround
/**
* @typedef {object} DestructuredVariable
* @property {string} total
*/
/** @type {DestructuredVariable} */
const {total} = getUser();
If you want to document an object the simple answer is:
/**
* @type {{total: String}}
*/
let { total } = settings;
for further details just take a look at the JS-Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With