TypeDoc Documentation

repository·master·Indexed 27 days ago

https://github.com/typestrong/typedoc

A documentation generator specifically designed for TypeScript projects that parses source code to generate structured HTML or JSON documentation. It automatically documents exported variables, functions, and classes using JSDoc-style comments, and supports rich features such as Markdown integration, GitHub-style alerts, MathML, and advanced declaration references for linking project members.

Tokens
46.1K
Snippets
188
Records
271
Agent score
94%

What's inside TypeDoc

  1. Overview of TypeDoc features

    master

    TypeDoc provides several features for generating high-quality documentation from TypeScript source code:

    • Automatic Documentation: Captures all exported variables, functions, classes, and more.
    • Markdown Support: Allows you to use Markdown syntax within your doc comments for rich text formatting.
    • Syntax Highlighting: Automatically provides syntax highlighting for code blocks within your documentation.
    • Language Construct Support: Built-in support for various TypeScript constructs including generics, overloads, interfaces, enums, and React components.
  2. Understand Declaration References in TypeDoc

    master

    TypeDoc uses declaration references for tags like {@link} and {@inheritDoc} to name other members in your documentation. These references are based on the TSDoc specification but modified to match the TypeScript language service resolution (similar to VSCode).

    A declaration reference consists of three optional parts:

    1. Module Source: An optional name followed by a ! (e.g., moduleA!). This refers to a specific module in multi-entry point sites.
    2. Component Path: One or more names separated by delimiters (., #, or ~) used to navigate the project tree.
    3. Meaning: An optional part used to disambiguate between multiple declarations (e.g., an overload or a specific type like class or interface).

    Note: If the --useTsLinkResolution option is enabled (default), TypeDoc will defer to TypeScript's resolution if TypeScript can successfully parse the link. TypeDoc's custom resolution logic is primarily used when TypeScript fails to parse the link or for external documents and README files.

  3. Configure TypeDoc input, output, and parsing options

    master

    TypeDoc provides several categories of options to customize documentation generation:

    • Configuration Options: Control which files TypeDoc reads.
    • Input Options: Control how input source code is converted into a project structure for HTML or JSON rendering.
    • Output Options: Control the generation and appearance of TypeDoc's HTML output.
    • Comment Options: Control how TypeDoc parses code comments and documentation blocks.
    • Organization Options: Control how content is organized within the converted project.
    • Validation Options: Configure the validation checks performed on the converted project.
    • Other Options: Miscellaneous settings.
  4. Use the {@inheritDoc} tag to copy documentation

    master

    The {@inheritDoc} tag allows you to create documentation for a reflection by copying it from another reflection. This is useful for interfaces or classes that implement or extend existing structures and should share the same documentation.

    The tag follows the form {@inheritDoc ref}, where ref is a declaration reference to the source documentation.

    Copied Elements: Following the TSDoc specification, only the following parts are copied:

    • The summary
    • The @remarks block
    • Any @param blocks
    • Any @typeParam blocks
    • The @returns block
    /**
     * Some documentation
     */
    export class SomeClass {}
    
    /** {@inheritDoc SomeClass} */
    export interface SomeUnrelatedClass {}
  5. Escape special characters in Doc Comments

    master

    To include literal characters that normally trigger parsing (like {, }, @, or /), use a backslash \ to escape them. All other escapes are passed through to markdown-it for processing. This is particularly useful for escaping the end of a comment block (e.g., \*/) when documenting code that contains its own comment blocks.

    /**
     * This is not a \@tag. Nor is this an \{\@inlineTag\}
     *
     * It is possible to escape the end of a comment:
     * ```ts
     * /**
     *  * docs for `example()`
     * \*/
     * function example(): void
     * ```
     */
  6. Include external markdown documents using the @document tag

    master

    You can attach standalone .md files to specific types (classes, functions, etc.) by using the @document tag in their doc comments. The path provided must be relative to the file containing the comment.

    /**
     * @document documents/external-markdown.md
     */
  7. Generate documentation for Monorepos / Workspaces

    master

    If your codebase consists of multiple npm packages, you can build documentation for each individually and merge them into a single site. To do this, set the entryPointStrategy option to packages.

    Note: In this mode, TypeDoc requires configuration to be present in each directory to specify the entry points.

  8. Use the @remarks tag for detailed documentation

    master

    The @remarks tag is a block tag used to separate a brief summary from more detailed, extensive documentation within a comment.

    Key behaviors:

    • Limit: At most one @remarks block is permitted per comment.
    • Inheritance: Unlike most other tags, the contents of an @remarks block are copied when using the {@inheritDoc} tag.
    • Theme Support: While the default TypeDoc theme simply displays @remarks content under a # Remarks header, other themes may use this tag to distinguish between summary-level content and detailed content intended for dedicated documentation pages.
    /**
     * Some docs here
     *
     * @remarks
     * Much longer documentation here
     */
    export function rand(): number;
  9. Define a custom theme

    master

    Themes are defined by plugins calling the defineTheme method on Application.renderer during the load phase. To create a theme that duplicates the default, you can pass DefaultTheme to defineTheme. To customize the theme, provide a class that overrides getRenderContext to return a custom context class (extending DefaultThemeRenderContext). This allows you to override specific template functions like footer.

    import { Application, DefaultTheme, JSX, PageEvent, Reflection } from "typedoc";
    
    class MyThemeContext extends DefaultThemeRenderContext {
        // Important: If you use `this`, this function MUST be bound!
        override footer = (context) => {
            return (
                <footer>
                    {context.hook("footer.begin", context)}
                    Copyright 2024
                    {context.hook("footer.end", context)}
                </footer>
            );
        };
    }
    
    class MyTheme extends DefaultTheme {
        getRenderContext(pageEvent: PageEvent<Reflection>) {
            return new MyThemeContext(this, pageEvent, this.application.options);
        }
    }
    
    export function load(app: Application) {
        app.renderer.defineTheme("open-web-analytics", MyTheme);
    }
  10. Use Markdown and Code Blocks in Doc Comments

    master

    TypeDoc uses a minimal parser to extract TSDoc/JSDoc tags and supports Markdown within comments. It uses Shiki for syntax highlighting in fenced code blocks.

    Key constraints:

    • Only fenced code blocks are supported. Indentation-based code blocks will not prevent tags from being parsed.
    • TypeDoc ignores any comments containing @license or @import tags.
    • You can customize syntax highlighting themes using the --lightHighlightTheme and --darkHighlightTheme options.
    • To use languages not loaded by default, use the highlightLanguages option.
    /**
     * This comment _supports_ [Markdown](https://www.markdownguide.org/)
     */
    export class DocumentMe {}
    
    /**
     * Code blocks are great for examples
     *
     * ```ts
     * // run typedoc --help for a list of supported languages
     * const instance = new MyClass();
     * ```
     */
    export class MyClass {}