How to document destructured variable with jsdoc

一笑奈何 提交于 2021-02-07 16:24:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!