如何在JSDoc中记录结构化参数?

问题描述 投票:0回答:1
async function userInformation({ userId, token }) {
  const body = {
    user: userId
  };

  const headers = {
    Authorization: `Bearer ${token}`,
    'Content-Type': 'application/x-www-form-urlencoded'
  };

  const url = 'https://example.com/api/users';

  const data = await axios.post(url, headers, qs.stringify(body));

  return data;
}

考虑此代码我应该如何为此功能编写jsdoc?如何确保在jsdoc中定义了参数类型?

javascript node.js documentation jsdoc
1个回答
0
投票

即使您解构了参数,它们仍然来自一个需要记录的来源(一个对象)。

我建议使用@typedef描述对象的形状,并在记录函数时将其用作类型。

/**
 * @typedef {object} Credentials
 * @property {number} userId
 * @property {string} token
 */

/**
 * @param {Credentials} credentials
 */
async function userInformation({ userId, token }) {
  // ...
}

这里是VS Code的屏幕录像,显示它可以解释此doc块。 (我确定其他IDE可以做到这一点)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.