HOW-TO · DEV
How to use AI to generate JSDoc and TypeDoc comments for a JavaScript or TypeScript codebase
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES
JS/TS codebase, AI coding assistant (Claude or similar), JSDoc or TypeDoc installed
What this does
This guide describes a workflow for using an AI assistant to generate JSDoc (for JavaScript) or TypeDoc (for TypeScript) comment blocks on functions, classes, interfaces, and modules. Proper documentation comments improve IDE autocomplete, enable documentation generation, and make the codebase easier to onboard new contributors to.
Steps
- Identify the files or functions that lack documentation. A quick scan using
grep -r "@param\|@returns" src/can reveal undocumented functions. - Open the target file in the editor or prepare a batch selection of files.
- Issue a targeted instruction to the AI assistant: "Add JSDoc comments to all exported functions in this file. Include @param, @returns, and @throws where applicable."
- Review the proposed comment blocks. Verify they accurately describe parameter types, return types, and side effects by cross-referencing the function implementation.
- Correct any inaccurate type annotations in the AI-generated comments before accepting.
- Accept the changes and move to the next file.
- Run TypeDoc to generate HTML documentation:
npx typedoc src/ --out docs/api. - Verify the generated documentation renders correctly by checking a sample page in the output directory.
Verification
npx typedoc src/utils/validator.ts --out /tmp/docs-check && ls /tmp/docs-check/index.html
# Expected output: /tmp/docs-check/index.html
Common failures
- Inaccurate type annotations in comments: The AI infers a wrong type from the function name or context. For example, annotating a parameter as
stringwhen the code actually expects a number. Solution: always cross-check AI-generated types against the TypeScript type definitions or runtime behavior. - Missing @throws documentation for error paths: The AI documents the happy path but omits possible exceptions. Solution: explicitly ask the AI to "document all error conditions and exceptions" and review the function body for try/catch blocks and thrown errors.
- Stale comments after refactoring: AI-generated comments become outdated when the function signature or behavior changes. Solution: treat documentation as a separate review step; run a linter that flags mismatched JSDoc types against actual signatures (e.g.,
ts-checkin TypeScript). - Overly verbose comments: The AI generates wall-of-text descriptions that hurt readability. Solution: instruct the AI to keep comments concise: one line per parameter, one line for returns.