javascript - 如何在 JSDoc 中记录解构参数?

标签 javascript node.js documentation jsdoc

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 中定义?

最佳答案

这并不完美,因为它是一个 open issue但你能做的最好的是:

/**
 * @param {{ userId: number, token: string }} info
 * @param {string} info.userId this description appears
 * @param {number} info.token this also appears on hover
 */
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;
}

你最终写了两次信息,但它似乎确保 VSCode 知道发生了什么。

关于javascript - 如何在 JSDoc 中记录解构参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58410776/

相关文章:

javascript - NodeJS JWT token 验证

javascript - Mongoose findById() 回调仅返回无意义的数据

node.js - 如何使用 Express 构造 "Client Credentials Flow"来授权使用 Spotify API?

haskell - 如何指示 haddock 链接到 hackage 软件包的 hackage 文档?

matlab - 在构造函数帮助文件中显示用户创建的 MATLAB 对象的可用方法

c# - 代码实体引用 SandcaSTLe developerHowToDocument 上的方法

javascript - JQuery 切换删除

javascript - 新 D3 API 中 '.orient()' 的替代项是什么?

javascript - 无法从 firebase 获取数据到我的 html 页面

javascript - Node.js "built-in"流会发出多个错误吗?