如何使用jsDoc正确记录类型为“Object”的Vue属性?

3
我想要实现的是记录类型为“Object”的属性,例如:
props: {
    foo: {
        type: Object,
        required: true
    }
}

现在这个对象foo可以并且将会有多个条目,其中一些是必填的,而其他的则是可选的。例如:
<my-component 
   :foo="{
        mandatoryItem1: 'bar',
        mandatoryItem2: {
            mayBeEvenNested: true
        },
        optionalItem1: 'baz',
        ...
    }"
/>

问题在于当使用属性时,您必须检查组件的源代码才能知道哪些条目必须存在于属性中。 虽然使用TypeScript消除这个问题会很好,但我们现在不能切换到TS,因此我们必须使用jsDoc。而且我们想以正确的方式做到这一点。
在记录函数及其参数时,您可以像这样描述嵌套参数对象(或类似对象):
/**
 * @param bar {Object} object containg...
 * @param bar.mandatoryItem1 {String} ...
 * @param bar.mandatoryItem2 {Object} ...
 * @param bar.mandatoryItem2.mayBeEvenNested {Boolean} ...
 */
function foo(bar) {

}

我如何为vue属性做类似的事情?

理想情况下,我们希望将来使用类似于vue-styleguidist的工具,使用jsDoc自动生成组件文档,因此重要的是使用“正确”的语法。

PS:我们也想为类型为“Array”的属性做同样的事情,即描述数组中对象的外观。

1个回答

1
你应该有类似这样的东西:
/**
 * @typedef {Object} Bar
 * @param {String} mandatoryItem1
 * @param {OtherObj} mandatoryItem2
 */

export default {
  name: 'my-component',
  props: {
    /** @type {Bar} */
    foo: { type: Object, required: true }
  }
}

其中OtherObj是另一种类型定义。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接