How can one point to a parameter within the documentation of a TypeScript function?

I am attempting to incorporate parameter references in function descriptions like this:

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

Unfortunately, it seems that using {@param id} does not yield the desired result. When invoking the function, the output is as follows:

(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

I would like to have a clickable element in the documentation that links to the parameter (within the function's general description, rather than within the parameter description). Is there a correct way to reference a parameter or even the return value of a function in the description? (I am utilizing Visual Studio Code).

Answer №1

April 2023, VSCode version 1.73.1

{@link paramname} is presented as a clickable link in the annotation popup of VSCode. Clicking on it will navigate to the referenced parameter.

Here is an example showcasing the use of @returns tag with @link parameter references:

/**
 * 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 the 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);
}

The documentation popup in VSCode appears as follows: https://i.sstatic.net/rKOm8.png

Answer №2

according to the advice from @basarat, it is not possible to directly reference a parameter within documentation itself. However, one option that comes close is using the @link and @see directives.

// this `id` const is used only when there's no @param id in the documentation
const id: number = 33 // or any other value
/**
 * Removes the Travel Expense with the specified {@link id}
 * @param id the identifier of the travel cost to be removed
 */
deleteTravelCost(id: number): Observable<{}> { [...] }

Answer №3

Is it possible to correctly reference a parameter?

Unfortunately, you cannot create cross-references for parameters. Refer to this link for more information: https://github.com/jsdoc/jsdoc/issues/1145#issue-126426000

How to include the return of a function in the description?

Once again, referencing the return value is not supported. However, you can document it using the 'returns' tag. Here's an example:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for resolving type checking errors in TypeScript and React

I encountered an error and would appreciate any assistance in resolving it. My tech stack consists of react, typescript, and Material UI. I am attempting to customize a button using the following link: https://mui.com/material-ui/customization/how-to-custo ...

Is there a way to define an interface that consists of child objects as the type for a function that uses destructured props?

Is there an alternative to this method? import { client } from "./setupApi"; export const getLayout = ({ page, entity, list }: {page: string, entity: string, list: string}) => { return client.get("/secure/nav.json"); }; How do I ...

Leverage the extended properties of Express.js's Request's generic arguments

I am currently working on extending the Request type to include a property that relies on the request body. However, I am facing an issue where the generic in catchAsync is not being correctly applied and always defaults to any instead of the specified gen ...

Leveraging Global Variables in TypeScript with Node Express

Having issues using global variables in node express when working with typescript. Specifically, trying to use the same global array variable tokenList:tokList across multiple modules and middleware, but haven't been successful so far. Any suggestions ...

Invoking the callback function within the containing scope in Typescript

I am facing an issue with my Angular component where I have a class that includes common services and functions. While passing some functions as callbacks, the scope is getting lost during execution. Let me demonstrate the problem through the code below: @ ...

What is the best way to specify a function parameter as the `QUnit` type using TypeScript in conjunction with QUnit?

In my project, which is partially written in TypeScript and licensed under MIT, I am utilizing QUnit. I have some TypeScript functions that require QUnit as a parameter, and I would like to define their types based on its interface from the typings. For e ...

Loading the value of a Subject variable in an Angular 2 application using Typescript

I am currently developing an Angular2 application where I am loading data from a service into my component as a subject. public type1Choisi: any; constructor( public formeService: FormeService, ...) { this.formeService._type1.subscribe(type1 => ...

Find the combined key names in an object where the values can be accessed by index

I am currently working on creating a function called indexByProp, which will only allow the selection of props to index by if they are strings, numbers, or symbols. This particular issue is related to https://github.com/microsoft/TypeScript/issues/33521. ...

Learn how to selectively hide the header from the root layout on a single page in Next.js version 14

I am currently working on an app using Next.js 14 and TypeScript. In my root layout, I have added a header and footer. However, on one of the pages I created, I need to hide the header and footer. import type { Metadata } from "next"; import { In ...

Continuously extract information by filtering through the array

Currently, I am in the process of developing an idle RPG game using Angular. One of the features I have implemented is a console log that displays events such as Damage Dealt and Experience Earned. In order to manage messages efficiently, I have created a ...

Hold off on proceeding until the subscription loop has finished

Is there a way to verify that all results have been successfully pushed to nodesToExpand after running the for loop? The getFilterResult function is being called via an HTTP request in the nodeService service. for(var step in paths) { this.nodeSe ...

Issue encountered while setting up controls and AbstractControls in form development

Here is a snippet of code showing how I create and manipulate a form in Angular: this.myForm = new FormGroup({ points: new FormArray([ new FormGroup({ date: this.date, startTime: new FormControl(null, Val ...

Tips on preventing the copying of .txt and .xml files with the fs-extra.copySync function

Currently, I am working on a small TypeScript assignment and facing an issue that I can't seem to solve. Any guidance or advice on the problem mentioned below would be greatly appreciated. The task at hand involves copying a directory from one locati ...

Learn the process of setting up a connection between Express JS and the NotionAPI using both POST and

As someone who is new to backend development, I am currently working on integrating a simple Notion form into a Typescript website. To guide me through the process, I found a helpful tutorial at . The tutorial demonstrates how to send data from localhost:3 ...

What specific event do I require for the onChange event in React using TypeScript?

I'm facing a problem while working with React TypeScript. I need to type the onChange event for a select element, but the data is coming from event.value instead of event.target.value. What should be the appropriate event to use in this case? Below i ...

What is the best way to retrieve and showcase data in NextJs version 13 and beyond?

Being new to NextJS, my question may seem trivial but I'd appreciate your patience. Essentially, my goal is to fetch data from a database and display it on the page upon the initial render. To achieve this, I am utilizing the useEffect and useState ho ...

Is it possible to integrate TypeScript 5.0 decorators into React components?

Every time I add decorators to my class, they always get called with the arguments specified for legacy decorators: a target, property key, and property descriptor. I am interested in using TypeScript 5.0 decorators. Is this feasible, and if so, how can I ...

In a Typescript Next Js project, the useReducer Hook cannot be utilized

I'm completely new to Typescript and currently attempting to implement the useReducer hook with Typescript. Below is the code I've written: import { useReducer, useContext, createContext } from "react" import type { ReactNode } from &q ...

Determining the name of the currently focused DOM element in Angular2

How can I detect the name of a selected element from a group of select elements on a page? For example: <select (click)="functionDetectName()" name="test1"> <select (click)="functionDetectName()" name="test2"> <select (click)="functionDete ...

Can we verify the availability of an Angular library during runtime?

I am in the process of creating a custom Angular library, let's name it libA, which has the capability to utilize another Angular library, named libB, for an optional feature. Essentially, if the Angular application does not include libB, then the fea ...