英文:
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
有几种解决方案:
- 使用泛型类型作为参数
/**
* 根据给定的索引查找页面并返回:
* - 如果不存在,则返回索引
* - 如果用户没有读取权限,则返回用户
* - 否则返回页面本身
*
* @template {number} IndexType
* @template {string} UserType
* @param {IndexType} index
* @param {UserType} user
* @returns {IndexType|UserType|Page}
*/
function getRead(index, user) {
...
}
- 完全移除函数的 JSDoc,依赖 TypeScript 的类型推断
function getRead(/**@type {number}*/index, /**@type {string}*/user) {
...
}
英文:
There are several solutions:
- 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) {
...
}
- Remove JSDoc for the function at all and rely on TypeScript's type inference
function getRead(/**@type {number}*/index, /**@type {string}*/user) {
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论