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 docwithout specifying a file or module will show an error. - Private members are not shown unless you use the
--privateflag. - Ensure your code compiles without errors; otherwise,
deno docmay 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:
| Option | Description |
|---|---|
deno doc <file_or_module> | Generate documentation for a file or module. |
--json | Output documentation in JSON format. |
--private | Include private members in the output. |
--unstable | Use 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.