How to Document TypeScript and JavaScript Code

TypeScript gives you a head start on documentation: every type annotation is a fact about the code that the compiler checks for you. That head start is also why so many TypeScript projects stop there. Types say what shape a value has. They do not say why a function exists, what its edge cases are, or how it is meant to be called, and that is the part people go looking for.

This guide covers each layer of documentation in a TypeScript or JavaScript codebase and how to keep it honest as the code moves.

Layer 1: Types are documentation the compiler checks

Start by letting the types carry everything they can:

type SizeUnit = "KB" | "MB";

function formatSize(bytes: number, unit: SizeUnit): string {
  // ...
}

A reader now knows unit can only be one of two values without reading a word of prose, and the compiler will reject any caller that disagrees. That property, documentation that fails loudly when it is wrong, is worth more than any comment. Prefer a union type over a string with a comment listing the allowed values.

In plain JavaScript you can get much of the same benefit with JSDoc type annotations and // @ts-check at the top of the file, which asks the TypeScript checker to verify them:

// @ts-check

/**
 * @param {number} bytes
 * @param {"KB" | "MB"} unit
 * @returns {string}
 */
function formatSize(bytes, unit) {
  // ...
}

Layer 2: Doc comments for what types cannot say

Doc comments are block comments that start with /**, placed directly above a declaration. Editors show them on hover, which makes them the most-read documentation in a TypeScript project.

The common tags, shared by JSDoc and the TSDoc standard:

/**
 * Formats a file size for display, rounded to one decimal place.
 *
 * @param bytes - The size in bytes, not kilobytes.
 * @param unit - Which unit to show the size in.
 * @returns The formatted size, for example "1.5 KB".
 *
 * @example
 * formatSize(1536, "KB"); // "1.5 KB"
 *
 * @deprecated Use `formatBytes`, which picks the unit for you.
 */
export function formatSize(bytes: number, unit: SizeUnit): string {

What earns its place in a doc comment:

In TypeScript, skip the types in @param tags. They are already in the signature, and a second copy is one more thing to drift.

Layer 3: Module, package and README docs

Every package benefits from a README that explains what it is for, how to install it, and a short example of the main use. Large modules benefit from a comment at the top explaining what lives in the file and when to reach for it. These are the docs a new contributor reads first, and the ones most often missing.

Layer 4: Generated reference pages

Once doc comments exist, a documentation site generator can turn your declarations and their comments into a browsable reference site, for TypeScript and plain JavaScript alike. For a library, an API report checked in CI is also worth having: it catches an accidental change to the public surface before it ships.

These tools present what you wrote very well. They cannot write the comments, and they cannot tell you when one no longer matches the function under it.

Generating documentation for undocumented code

Most codebases have far more undocumented functions than anyone will annotate by hand. A documentation generator covers that backlog.

Writulos Scribe reads a TypeScript or JavaScript file (.ts, .tsx, .js, .jsx and more) and writes structured markdown reference docs for it: an overview of what the file does and how it works, then a section for each function, class or component with a plain-English description, parameters with types, the return value and a usage example, plus dependencies and notes. It documents only what is in the code, so it does not invent behavior, and it reads your types and existing doc comments along with everything else in the file.

It writes separate markdown files rather than editing your source, so nothing lands in your code without review. Paste a file into the app to see the output with no account, or run it on every push with the GitHub Action. If you work in Cursor, Claude Code or Claude Desktop, the MCP server documents files from the chat.

Keeping it current

Comments drift like any other documentation. A parameter changes meaning, the comment keeps describing the old one, and the compiler has no opinion about prose. A few habits help:

  1. Move facts into types wherever you can, so the compiler checks them.
  2. Lint your doc comments. eslint-plugin-jsdoc can require them on exported functions and flag @param names that no longer match the signature.
  3. Keep examples runnable, ideally as tests, so an example that stops working fails the build.
  4. Measure drift for generated docs. Scribe stamps each doc with the commit it was written from and counts the commits that have touched the source since, so you can see which docs are behind. On Pro and Business it can fail the pull request until they are current.

A checklist to start with

Types tell readers what the code accepts. Documentation tells them what it is for. A good TypeScript codebase has both, and has a way to notice when either is wrong.