13

Assuming a generic type below, what should I use in place of ??? to properly document T?

/**
 * ??? The description of T.
 */
class MyClass<T> {
}

In C# I'd use <typeparam>. Is there an official JSDoc equivalent?

5
  • Related?: Document generic type parameters in JSDOC Commented Nov 15, 2021 at 8:15
  • @VLAZ Yeah I saw that, but It's unclear how it applies to my case. What should I write instead of ??? above? Commented Nov 15, 2021 at 8:16
  • It's also unclear to me, to be honest. Hence the question mark. I've read it but I'm not sure how it applies to TS. Commented Nov 15, 2021 at 8:17
  • 1
    Looking at some more sources, perhaps all you need is @template T - some description here see here (it's TS documentation but explains how to annotate JS code) and here is the full description of @template. Annoyingly the JSDoc documentation doesn't mention @template Commented Nov 15, 2021 at 8:25
  • @VLAZ That could be it, thanks. Commented Nov 15, 2021 at 8:28

3 Answers 3

18

VLAZ notes in the comments that @template works but isn't mentioned in the official JSDoc documentation. It is, however, mentioned in Typescript's own JSDoc reference:

You can declare type parameters with the @template tag. This lets you make functions, classes, or types that are generic:

Example:

/**
 * Description of the class MyClass.
 * 
 * @template T Description of the type parameter T.
 */
class MyClass<T> {
    constructor(public readonly x: T) {}
}

The text "Description of the type parameter T." appears in the Typescript Playground when I hover over the occurrence of T in the constructor, so this does seem to work.

Playground Link


If instead of JSDoc you want to use TSDoc, then the tag is @typeParam.

Sign up to request clarification or add additional context in comments.

Comments

4

for those who are looking the TsDoc, you can use the https://tsdoc.org/pages/tags/typeparam/

1 Comment

Great! In Intellij, the '@typeParam' displays the description as I hover over the type, but the '@template' doesn't.
-1
/*
 * @typeParam T - Description of T
 */
class MyClass<T> {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.