JSDoc函数返回其自身的参数

huangapple go评论75阅读模式
英文:

JSDoc function returning a parameter of itself

问题

Sure, here's the translated code portion you requested:

/**
 * 找到具有给定索引的页面并返回
 *  - 如果不存在,则返回索引
 *  - 如果用户没有阅读权限,则返回用户
 *  - 否则返回页面本身
 * 
 * @param {number} index 
 * @param {string} user 
 * @returns {index|user|Page}
 */
function getRead(index, user) {
    let page = pages.find(page => page.details.index == index);
    if (page && page.canRead(user)) {
        return page;
    } else if (!page) return index;
    else return user;
}

Please note that JavaScript does not support the type of conditional return value you are attempting to document using JSDoc. The @returns tag in JSDoc typically describes the data type that the function returns, but it cannot be used to express conditions like {index|user|Page}.

英文:

I need to document a function returning conditionally one of its parameters. But JSDoc seems not to accept variables as a return value.

I tried to do something like this following the return type {1 | 0} logic

/**
 * Finds the page with given index and returns
 *  - index if it does not exist
 *  - user if user doesn't have permissions to read
 *  - else the page itself 
 * 
 * @param {number} index 
 * @param {string} user 
 * @returns {index|user|Page}
 */
function getRead(index, user) {
    let page = pages.find(page => page.details.index == index);
    if (page && page.canRead(user)) {
        return page;
    } else if (!page) return index;
    else return user;
}

But it is not recognized and the return values is just any, if i use {string|number|Page} as type, afterwards in my code I do something like this

if(page == index) return res.send(`Page ${index} does not exist`);
if(page == user) return res.send(`No permissions to view page ${index}`);
//page still recognized as number | string | Page
//no code completion or property suggestions on page

I also tried to add type checks to get there but i don't want to believe that like such a lazy the solution is the only one, there must be a better way to handle this

if(typeof page == "number" || page == index) return res.send(`Page ${index} does not exist`);
if(typeof page == "string" || page == user) return res.send(`No permissions to view page ${index}`);
//page recognized as Page

答案1

得分: 1

有几种解决方案:

  1. 使用泛型类型作为参数
/**
 * 根据给定的索引查找页面并返回:
 *  - 如果不存在,则返回索引
 *  - 如果用户没有读取权限,则返回用户
 *  - 否则返回页面本身
 * 
 * @template {number} IndexType
 * @template {string} UserType
 * @param {IndexType} index 
 * @param {UserType} user 
 * @returns {IndexType|UserType|Page}
 */
function getRead(index, user) {
 ...
}
  1. 完全移除函数的 JSDoc,依赖 TypeScript 的类型推断
function getRead(/**@type {number}*/index, /**@type {string}*/user) {
  ...
}

JSDoc函数返回其自身的参数

JSDoc函数返回其自身的参数

英文:

There are several solutions:

  1. Use generic types for parameters
/**
 * Finds the page with given index and returns
 *  - index if it does not exist
 *  - user if user doesn't have permissions to read
 *  - else the page itself 
 * 
 * @template {number} IndexType
 * @template {string} UserType
 * @param {IndexType} index 
 * @param {UserType} user 
 * @returns {IndexType|UserType|Page}
 */
function getRead(index, user) {
 ...
}

JSDoc函数返回其自身的参数

  1. Remove JSDoc for the function at all and rely on TypeScript's type inference
function getRead(/**@type {number}*/index, /**@type {string}*/user) {
  ...
}

JSDoc函数返回其自身的参数

huangapple
  • 本文由 发表于 2023年1月5日 16:41:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75015756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定