Adding documentation for hovers

Note: This is kind of a reminder to myself and a way for me to have this knowledge put somewhere else.

A TSDoc comment is a multiline comment that documents the purpose and usage of your code.

In this case, we’ll specify that the function “Adds two numbers together”. We can also use an @example tag to show an example of how the function is used:

/**
 * Adds two numbers together.
 *
 * @example
 *
 * sum(1, 2);
 * // Will return 3
 */
const sum = (a: number, b: number) => {
	return a + b;
};

Now whenever you hover over this function, the signature of the function along with the comment and full syntax highlighting for anything below the @example tag:

sum(3, 4);
// hovering over sum shows:

const sum: (a: number, b: number) => number
Adds two numbers together.
@example
sum(1, 2);
// Will return 3

How it looks:

Image Description

The combination of comments and examples in TypeScript is a powerful way to communicate the purpose and usage of your code to others, whether you’re working on a library, on a team, or just for yourself.