有没有一种方法可以在TypeScript函数文档中引用参数?

qvk1mo1f  于 2023-04-13  发布在  TypeScript
关注(0)|答案(3)|浏览(156)

我尝试在函数的描述中引用参数,如下所示:

/**
 * Deletes the Travel Cost with the given {@param id}
 * @param id the id of the travel cost to be deleted
 */
deleteTravelCost(id: number): Observable<{}> { [...] }

但它似乎不适用于{@param id}。调用该函数时的结果如下:

(method) TravelCostsService.deleteTravelCost(id: number): Observable<{}>

Deletes the Travel Cost with the given {@param id}

@param id — the id of the travel cost to be deleted

我希望在文档中有一个可点击的元素引用参数(在函数的一般描述中,而不是在实际的参数描述中)。有没有合适的方法在描述中引用参数甚至函数的返回值?(我使用Visual Studio Code)。

pqwbnv8z

pqwbnv8z1#

有没有一种方法可以正确引用参数
不能交叉引用参数:https://github.com/jsdoc/jsdoc/issues/1145#issue-126426000
返回描述中的函数
同样,你不能引用它。但是你可以记录它with returns,例如:

/**
 * Deletes the Travel Cost with the given 
 * @param id the id of the travel cost to be deleted
 * @returns the result
 */
function deleteTravelCost(id: number): number {
  return id;
}
roejwanj

roejwanj2#

正如@basarat所说,没有办法在文档本身中交叉引用参数,所以我能想到的最接近的方法是@link和@see指令

// this `id` const is referenced only when there's no @param id in doc
const id: number = 33 // or anything
/**
 * Deletes the Travel Cost with the given {@link id}
 * @param id the id of the travel cost to be deleted
 */
deleteTravelCost(id: number): Observable<{}> { [...] }
0g0grzrc

0g0grzrc3#

2023年4月,VSCode v1.73.1
{@link paramname}将在VSCode的注解弹出窗口中显示为链接,单击它将指向引用的参数。
@returns标记包含@link参数引用的示例:

/**
 * Returns the original collection or filters it before returning
 * @param collection the collection to filter
 * @param filter if `false`, don't filter; otherwise the passed value is a predicate function and will be used for filtering
 * @returns the original {@link collection} with some or all of the original items depending on passed {@link filter}
 */
export function optionalFilter<T>(collection: T[], filter: ((value: T, index?: number, array?: T[]) => boolean) | false) {
    if (filter === false) {
        return collection;
    }
    return collection.filter(filter);
}

VSCode文档弹出窗口如下所示:

相关问题