Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: Are there any conventions to document code with comments?

I am used to document code in our C# projects in a specific way to enhance team productivity, benefit from Intellisense in Visual Studio etc.

Code looks similar to this:

/// <summary>
/// Loads a user with a specific id.
/// </summary>
/// <param name="id">The id of the user to search for.</param>
/// <returns>A user with the given id.</returns>
public User GetUserById(string id) {
    ...
}

Are there any similar conventions for Typescript for commenting and documentation? Or even tools that use these conventions to generate documentation pages in html from code comments (like JavaDoc)?

like image 739
Michael Hilus Avatar asked Mar 22 '17 10:03

Michael Hilus


People also ask

Can I use JSDoc in TypeScript?

JsDoc is an API documentation generator for JavaScript using multi-line comments. It scans through your code and generates documentation in available in a HTML website. JsDoc is compatible with both JavaScript and TypeScript.

How do you write comments in JSDoc?

Create JSDoc commentsPosition the caret before the declaration of the method/function or field to document, type the opening block comment /** , and press Enter . WebStorm generates a JSDoc comment with a list of parameters ( @param ) and return values ( @returns ), where applicable.

What is JSDoc used for?

JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating.


2 Answers

Yes there are.

Most common used comment conventions (to no surprise) comes from javascript in form of jsdoc. For example VSCode support them out of the box. Also there are some tools specifically developed for typescript doc generation like typedoc

like image 165
Amid Avatar answered Sep 22 '22 11:09

Amid


TSDoc is the latest proposed convention for commenting and documentation of Typescript source file. Its notation looks as follows -

/**
 * Returns the average of two numbers.
 *
 * @remarks
 * This method is part of the {@link core-library#Statistics | Statistics subsystem}.
 *
 * @param x - The first input number
 * @param y - The second input number
 * @returns The arithmetic mean of `x` and `y`
 */
function getAverage(x: number, y: number): number {
  return (x + y) / 2.0;
}

TypeDoc tool can parse comments in this convention & generates documentation pages in HTML.

like image 44
Mohit Khandelwal Avatar answered Sep 22 '22 11:09

Mohit Khandelwal