【问题标题】:How to document destructured function arguments? e.g. `a` when writing `const fn = ({a}) => {/* code */}`如何记录解构函数参数?例如`a` 当写 `const fn = ({a}) => {/* code */}`
【发布时间】:2020-12-18 08:16:03
【问题描述】:

我写了很多代码,比如:

const MyFn = ({myInput}) => {
   // code
};

我应该如何记录这些输入的类型?

如果我不使用解构,我会这样写:

/**
* @param {Number} myArg It's a number
*/
const MyFn = (myArg) => {
   // code
};

...我的 IDE(VS 代码)会选择它并使用它来提出有用的建议。

({myInput}) 的解构语法的等效 jsdoc 是什么?

【问题讨论】:

    标签: javascript documentation jsdoc destructuring


    【解决方案1】:

    来自Documents with Properties:

    记录解构参数

    /**
     * Assign the project to an employee.
     * @param {Object} employee - The employee who is responsible for the project.
     * @param {string} employee.name - The name of the employee.
     * @param {string} employee.department - The employee's department.
     */
    Project.prototype.assign = function({ name, department }) {
        // ...
    };
    

    基本上,您为对象/数组添加一个@param,然后为您要从中解构的内容添加一个@param。

    因此将其应用于您的代码:

    /**
    * @param {Object} thingy - The description for the object you're expecting
    * @param {TypeForMyInput} thingy.myInput - The description of the destructured property
    */
    const MyFn = ({myInput}) => {
       // code
    };
    

    【讨论】:

      【解决方案2】:

      您也可以使用@typedef 来执行此操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-23
        • 1970-01-01
        • 1970-01-01
        • 2018-01-29
        相关资源
        最近更新 更多