0
0
DenoHow-ToBeginner ยท 4 min read

How to Use Deno Doc: Generate Documentation for Your Code

Use the deno doc command followed by a file or module path to generate documentation from your code comments. It extracts JSDoc comments and shows a structured summary of functions, classes, and types in your code.
๐Ÿ“

Syntax

The basic syntax of deno doc is:

  • deno doc [options] <file_or_module>: Generates documentation for the specified file or module.
  • --json: Outputs the documentation in JSON format.
  • --private: Includes private members in the documentation.

You can also specify URLs or local file paths.

bash
deno doc [options] <file_or_module>
๐Ÿ’ป

Example

This example shows how to generate documentation for a simple TypeScript file with JSDoc comments.

typescript
/**
 * Adds two numbers together.
 * @param a First number
 * @param b Second number
 * @returns The sum of a and b
 */
export function add(a: number, b: number): number {
  return a + b;
}

/**
 * A simple class representing a person.
 */
export class Person {
  /** The person's name */
  name: string;

  /**
   * Creates a new Person.
   * @param name The name of the person
   */
  constructor(name: string) {
    this.name = name;
  }
}
๐Ÿ’ป

Example Usage and Output

Run the following command in your terminal to see the documentation summary:

bash
deno doc example.ts
Output
example.ts export function add(a: number, b: number): number Adds two numbers together. export class Person A simple class representing a person. name: string The person's name constructor(name: string) Creates a new Person.
โš ๏ธ

Common Pitfalls

  • Not using proper JSDoc comments (use /** ... */ or /// for documentation).
  • Running deno doc without specifying a file or module will show an error.
  • Private members are not shown unless you use the --private flag.
  • Ensure your code compiles without errors; otherwise, deno doc may fail.
typescript
/* Wrong: Missing JSDoc style comment */
// Adds two numbers
export function add(a: number, b: number): number {
  return a + b;
}

/* Right: Proper JSDoc comment */
/**
 * Adds two numbers.
 * @param a First number
 * @param b Second number
 * @returns Sum of a and b
 */
export function add(a: number, b: number): number {
  return a + b;
}
๐Ÿ“Š

Quick Reference

Here is a quick summary of useful deno doc options:

OptionDescription
deno doc <file_or_module>Generate documentation for a file or module.
--jsonOutput documentation in JSON format.
--privateInclude private members in the output.
--unstableUse unstable Deno APIs if needed.
โœ…

Key Takeaways

Use deno doc <file_or_module> to generate documentation from your code comments.
Write clear JSDoc comments using /** ... */ or /// for best results.
Add --private to include private members in the documentation output.
You can output JSON format with --json for further processing.
Ensure your code compiles without errors before running deno doc.