我有这样的东西:
let { total } = settings;
如何记录总变量?我尝试过这样的事情:
/**
* @type {Object}
* @property {String} total.test
*/
let { total } = settings;
但这似乎不是正确的方法。
有什么建议吗?
答案 0 :(得分:1)
@Tommy-Pepsi Gaudreau对原始问题的评论如此接近。
这里是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;
警告数量: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-2018年9月27日:我减少了初始输入的数量,以确保/澄清类型不会被忽略,并且警告来自破坏结构中的类型。
答案 1 :(得分:0)
对于任何直接解构的变量,您可以尝试此解决方法
/**
* @typedef {object} DestructuredVariable
* @property {string} total
*/
/** @type {DestructuredVariable} */
const {total} = getUser();
答案 2 :(得分:0)
简单地内联JSDoc块在解构块中:
const {
/**
* The answer to everything
* @type {number}
*/
a,
/**
* The perfect food container
* @type {string}
*/
b,
/**
* Some other stuff
* @type {object}
* @property {string} foo Random text
* @property {boolean} baz Random flag
*/
c
} = {a: 42, b: 'burrito', c: { foo: 'bar', baz: true }};
VS Code 的截屏:
使用 yarn jsdoc file1.js
生成的文档