7

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?

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
  • Related?: [Document generic type parameters in JSDOC](https://stackoverflow.com/q/16017627) – VLAZ Nov 15 '21 at 08:15
  • @VLAZ Yeah I saw that, but It's unclear how it applies to my case. What should I write instead of `???` above? – Branko Dimitrijevic Nov 15 '21 at 08: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. – VLAZ Nov 15 '21 at 08:17
  • 1
    Looking at some more sources, perhaps all you need is `@template T - some description here` see [here](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#template) (it's TS documentation but explains how to annotate JS code) and [here is the full description of `@template`](https://github.com/google/closure-compiler/wiki/Generic-Types). Annoyingly [the JSDoc documentation doesn't mention `@template`](https://jsdoc.app/) – VLAZ Nov 15 '21 at 08:25
  • @VLAZ That could be it, thanks. – Branko Dimitrijevic Nov 15 '21 at 08:28

2 Answers2

12

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 (link):

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

kaya3
  • 47,440
  • 4
  • 68
  • 97
1

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

woss
  • 933
  • 1
  • 11
  • 20