Typescript: What is TSDoc?

The way to write the better comments

Suyeon Kang
suyeonme
4 min readJul 3, 2022

--

Photo by Monti Timpanogus on Unsplash

Writing good comments is pretty important when co-working with other developers in an organization. Although the best practice is not to leave any comments in the first place because the code is clear and easy enough to catch its functionality. But, sometimes, well-commented code can save plenty of time for the developers who read it and it leads to boosting their productivity. As a result, it makes code maintenance much easier.

So how can we document a comment well as a frontend developer? TSDoc might help you! I strongly recommend using this awesome guy. you might have heard about JsDoc which is a markup language used to annotate JavaScript source code files. TSDoc is based on JSDoc so it supports typescript extension as well as javascript.

What is TSDoc?

TSDoc is a proposal to standardize the doc comments used in TypeScript code, so that different tools can extract content without getting confused by each other’s markup. — tsdoc.org

TSDoc Example

TSDoc is doc comments for typescript code. You can define metadata and API documents for the code. Typescript provides static typing. By using this feature, you can document the detail of the code easily.

Visual Studio Code supports TSDoc syntax. If you leave comments using the syntax of TsDoc, the tag is automatically highlighted.

Syntax of TSDoc

Syntax is very simple. That’s all.

  • Comment should start with /**.
  • Tag uses @(e.g. @returns).

Types of TSDoc Tags

You can use tags to categorize the type of comment. I prepare some examples of the tags.

@deprecated: Used for deprecated code(APIs) which is no longer supported or maintained and may be removed in a future release.

@defaultValue: Used to document the default value for a field or property, if a value is not assigned explicitly (In case the value is optional).

@example: Used to document an example of the function with a code sample.

@experimental, @beta, @alpha, @public

These tags represent software release life cycle.

  • @experimental: Same as @beta, but used by tools that don’t support an @alpha release stage.
  • @alpha: In case of alpha release phase.
  • @beta: In case of beta release phase.
  • @public: In case of stable release phase.

What is software release life cycle?

A software release life cycle represents the stages of software development. You might be familiar with the terminologies like alpha, beta, or stable(production).

  • alpha: the first phase of software testing. The developers usually test the software using white-box, black-box or gray-box techniques and it is not thoroughly tested by the developer before it is released to customers.
  • beta: The phase following alpha and it begins when the software is feature complete but likely to contain a number of known or unknown bugs.
  • stable(production): The last phase which has passed all verifications and tests. This release goes to production.

@link: Used to create a hyperlink for references.

@override, @virtual

  • @override: In case a member function or property is overriding the definition inherited from the parent class.
  • @virtual: The parent class definition is be marked as @virtual

@param, @returns

  • @param: Used to document a function parameter.
  • @returns:Used to document a function’s return value.

@readonly: Used to document in case the value is readonly.

@remarks, @description, @summary: Used to document a description or summary about a function.

@see: Used to represent references which provide an information.

Conclusion

One day, I read an article saying that most developers use their time 90% to read and analyze existing code and 10% to write. Code commenting is helpful, especially in the case of the complicated and gigantic codebase.

--

--