Writing Documentation for a TypeScript Project: From A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Documentation is Needed in TypeScript Projects

\n

High-quality documentation is not just a nice bonus, but a critically important element of any professional TypeScript project. It serves as a bridge between developers, facilitates the onboarding of new team members, and helps avoid errors when using the API. TypeScript, with its powerful type system, provides unique opportunities for generating accurate and self-documenting documentation. In this article, we will break down the complete process: from writing comments in code to publishing the finished documentation.

\n\n

Basics: JSDoc and Annotations for TypeScript

\n

TypeScript supports JSDoc syntax, which allows you to add structured comments directly into the code. This is the foundation for creating documentation that is understandable to both humans and tools.

\n\n

Basic JSDoc Tags

\n

Let's start with a simple example of a function that adds two numbers:

\n
/**\n * Adds two numbers.\n * @param a - The first addend.\n * @param b - The second addend.\n * @returns The sum of two numbers.\n */\nfunction add(a: number, b: number): number {\n    return a + b;\n}
\n

Key tags:

\n
    \n
  • @param — describes a function argument. It is recommended to specify the type and purpose.
  • \n
  • @returns — describes the return value.
  • \n
  • @throws — indicates which exceptions the function can throw.
  • \n
  • @example — shows a usage example.
  • \n
\n\n

Documenting Complex Types

\n

For interfaces and types, use @property or describe fields inside the block:

\n
interface UserConfig {\n    /** User name */\n    name: string;\n    /** User age (optional) */\n    age?: number;\n    /** Role in the system */\n    role: 'admin' | 'user';\n}\n\n/**\n * Creates a user configuration.\n * @param config - Object with user settings.\n * @returns Configuration object.\n */\nfunction createUser(config: UserConfig): UserConfig {\n    return config;\n}
\n

Use @template for generics:

\n
/**\n * Wrapper for storing a value.\n * @template T - Type of the stored value.\n */\nclass Box<T> {\n    /** Stored value */\n    value: T;\n    constructor(value: T) {\n        this.value = value;\n    }\n}
\n\n

Tools for Documentation Generation: TypeDoc

\n

TypeDoc is the primary tool for automatically generating documentation from JSDoc annotations in TypeScript. It creates a static HTML site with convenient navigation.

\n\n

Installation and Setup

\n

Install TypeDoc as a dev dependency:

\n
npm install --save-dev typedoc
\n

Create a configuration file typedoc.json in the project root:

\n
{\n  "entryPoints": ["src/index.ts"],\n  "out": "docs",\n  "theme": "default",\n  "excludePrivate": true,\n  "excludeProtected": false,\n  "includeVersion": true,\n  "categorizeByGroup": true\n}
\n

Configuration parameters:

\n
    \n
  • entryPoints — the entry point to your project (usually the main export file).
  • \n
  • out — the folder where the documentation will be generated.
  • \n
  • excludePrivate — hide private class members.
  • \n
  • includeVersion — add the project version to the documentation.
  • \n
\n\n

Running Generation

\n

Add a script to package.json:

\n
{\n  "scripts": {\n    "docs:generate": "typedoc"\n  }\n}
\n

Execute the command:

\n
npm run docs:generate
\n

After this, a static site will appear in the docs folder. Open docs/index.html in your browser.

\n\n

Best Practices for Writing Documentation

\n

To make the documentation useful, follow a few rules.

\n\n

1. Write Comments

Blogs

Book Recommendations