46

I'm familiar with Javadoc. In Javadoc, you can place a link that refers to the Javadoc placed on another type like so:

/**
 * some java thingy. see this other java thingy too {@link OtherThingy}
 */
public class Thingy { /*...*/ }

/**
 * some other java thingy. see the first java thingy too {@link Thingy}
 */
public class OtherThingy{ /*...*/ }

Can I do the same in typescript's flavor of JSDoc? I know that I can use markdown in the comments and I can place web links but that's not exactly what I'm going for.

Also, any references to JSDoc/typescript documentation tools would be very helpful :)

Edit: Per the answers below, this is a feature of JSDoc but doesn't seem to be included in VSCode. Is there an valid syntax in VSCode?

Graham P Heath
  • 7,009
  • 3
  • 31
  • 45
Rico Kahler
  • 17,616
  • 11
  • 59
  • 85

3 Answers3

26

You sure can, though your mileage may vary.

1: A use of @link in Selenium-Webdriver's TypeScript typing file

/**
 * Converts a level name or value to a {@link logging.Level} value.
 * If the name/value is not recognized, {@link logging.Level.ALL}
 * will be returned.
 * @param {(number|string)} nameOrValue The log level name, or value, to
 *     convert .
 * @return {!logging.Level} The converted level.
 */
function getLevel(nameOrValue: string | number): Level;

2: Docs about @link in JSDoc

The following example shows all of the ways to provide link text for the {@link} tag: Providing link text

/**
 * See {@link MyClass} and [MyClass's foo property]{@link MyClass#foo}.
 * Also, check out {@link http://www.google.com|Google} and
 * {@link https://github.com GitHub}.
 */
function myFunction() {}

By default, the example above produces output similar to the following: Output for {@link} tags

See <a href="MyClass.html">MyClass</a> and <a 
href="MyClass.html#foo">MyClass's foo
property</a>. Also, check out <a 
href="http://www.google.com">Google</a> and
<a href="https://github.com">GitHub</a>.
Sufian
  • 6,405
  • 16
  • 66
  • 120
Graham P Heath
  • 7,009
  • 3
  • 31
  • 45
20

With VSCode 1.52 (Nov. 2020), you might also consider another tag:

Initial support for JSDoc @see tags

JSDoc @see tags let you reference other functions and classes in your JSDoc comments.

The example below shows crash function referencing the WrappedError class from another file:

// @filename: somewhere.ts
export class WrappedError extends Error { ... }

// @filename: ace.ts
import { WrappedError } from './somewhere'

/**
* @see {WrappedError}
*/
function crash(kind) {
   throw new WrappedError(kind);
}

VS Code will now include basic @see references while performing renames.

You can also run go to definition on the @see tag's content and @see tags will also show up in the list of references.

We plan to continue improving support for @see tags in future releases.

As Mark notes in the comments:

JSDoc @link support

We now support JSDoc @link tags in JavaScript and TypeScript comments.

These let you create clickable links to a symbol in your documentation:

Navigating code using JSDoc @link tags

JSDoc @link tags are written as: {@link symbolName}.

You can also optionally specify text to be render in place of the symbol name: {@link class.property Alt text}.

@link is supported in hovers, suggestions, and signature help.
We have also updated vscode.d.ts to use @link.


Note: cachius adds in the comments:

import { MyClassName } from "path/to/MyClassName";

If MyClassName is not already imported, adding @see MyClassName in the JSDoc would only show any on hover and doesn't allow ctrl + clickthrough to declaration/usages.

Importing it allows that, even if the only mention is in the JSDoc.
This is useful when a module/class refers to some other logically, but without using it directly.

Unused imports might trigger eslint warnings though, which I'd silence in this case with:

// eslint-disable-next-line @typescript-eslint/no-unused-vars 
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • better answer for 2020. nice – Rico Kahler Dec 08 '20 at 20:49
  • @Mark Good point, thank you. I have edited the answer to include that reference. – VonC Mar 29 '21 at 19:31
  • @link support in vscode: https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_57.md#jsdoc-link-support – Mark Jun 06 '21 at 15:03
  • @Mark Thank you. I have edited the answer accordingly. – VonC Jun 06 '21 at 15:10
  • `import { MyClassName } from "path/to/MyClassName";` if the symbol cannot be resolved. Please eslint with `// eslint-disable-next-line @typescript-eslint/no-unused-vars` if needed. – cachius May 04 '23 at 17:51
  • @cachius Is that an error message you see? Or something you had to apply? – VonC May 04 '23 at 18:43
  • If MyClassName is not already imported, adding `@see MyClassName` in the JSDoc would only show `any` on hover and doesn't allow `ctrl + click`through to declaration/usages. Importing it allows that, even if the only mention is in the JSDoc. This is useful when a module/class refers to some other logically, but without using it directly. Unused imports might trigger eslint warnings though, which I'd silence in this case. – cachius May 04 '23 at 19:29
12

VS Code treats {@link} as a comment, so it doesn't get any special parsing (it's just displayed exactly as you typed it). There's currently an open issue to fix this, however.

vaindil
  • 7,536
  • 21
  • 68
  • 127