TypeScript Documentation Verifier
typescript-docs-verifier is a utility that systematically verifies TypeScript code examples embedded within Markdown files (such as READMEs or documentation pages) to ensure they compile without errors. It addresses the common problem of outdated or incorrect code samples in documentation by extracting `typescript` and `tsx` code blocks, replacing project-specific imports for accurate compilation context, and reporting any TypeScript compilation failures. The current stable version is 3.0.2. While there isn't a strict release cadence, major version updates indicate significant development. Key differentiators include its focus solely on compilation correctness, support for ignoring specific code blocks, and its ability to integrate into build processes. It requires Node.js version 20 or higher and TypeScript version 4.7.2 or higher as a peer dependency.
Common errors
-
Error compiling example code block X in file Y: ... (TypeScript error message, e.g., 'TS2304: Cannot find name 'myVariable'.')
cause A TypeScript code block within a Markdown file contains valid TypeScript compilation errors, such as syntax errors, type mismatches, or references to undeclared variables or types.fixCarefully review the reported TypeScript error message and the corresponding code snippet in your Markdown file. Correct the TypeScript code to resolve the compilation issue, ensuring each snippet is self-contained and syntactically correct. -
The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("{{pkg-name}}")' call instead.cause Attempting to import `typescript-docs-verifier` using `require()` from a CommonJS module in a project where the library's primary entry point is configured as an ECMAScript Module (ESM) via `package.json` `exports`.fixUpdate your project's module system to use ESM `import` statements if possible. Alternatively, ensure your `tsconfig.json`'s `moduleResolution` is appropriately configured (e.g., `Node16` or `NodeNext`) and that your `package.json`'s `type` field or `exports` map correctly specifies CommonJS entry points if CJS compatibility is intended. -
TS2307: Cannot find module 'typescript-docs-verifier' or its corresponding type declarations.
cause The `typescript-docs-verifier` package is either not installed in the project, or TypeScript cannot resolve the module's path or type declarations due to incorrect `moduleResolution` settings in `tsconfig.json`, or issues with `package.json` `exports`.fixFirst, ensure the package is correctly installed (`npm install typescript-docs-verifier`). For TypeScript projects, verify that your `tsconfig.json`'s `moduleResolution` compiler option is set to a modern value like `Node16` or `NodeNext` to correctly interpret `package.json` `exports` fields for module lookup.
Warnings
- breaking Upgrading from `1.x` to `3.x` major versions introduces breaking changes, primarily due to updated Node.js and TypeScript ecosystem requirements. The package now explicitly requires `Node.js >=20` and `TypeScript >=4.7.2`.
- gotcha Each TypeScript code block in Markdown files is compiled in isolation. This means that declarations (variables, types, functions) from one snippet are not carried over or accessible to subsequent snippets within the same Markdown file.
- gotcha Both the command-line interface and library usage of `typescript-docs-verifier` default to locating `tsconfig.json` in the current working directory's root if the `--project` flag or `project` option is not explicitly provided. This default might not point to the `tsconfig` configured for your documentation examples.
- breaking Version 1.1.3 of the package included unspecified security fixes. Users still on older 1.x versions prior to 1.1.3 are exposed to potential vulnerabilities.
Install
-
npm install typescript-docs-verifier -
yarn add typescript-docs-verifier -
pnpm add typescript-docs-verifier
Imports
- compileSnippets
const { compileSnippets } = require('typescript-docs-verifier')import { compileSnippets } from 'typescript-docs-verifier' - SnippetCompilationResult
import { SnippetCompilationResult } from 'typescript-docs-verifier'
Quickstart
import { compileSnippets, SnippetCompilationResult } from 'typescript-docs-verifier';
const markdownFiles = ['README.md', 'docs/examples.md']; // Specify markdown files to check
const tsconfigPath = 'tsconfig.docs.json'; // Path to a tsconfig.json file for compilation context
compileSnippets({ markdownFiles, project: tsconfigPath })
.then((results: SnippetCompilationResult[]) => {
let hasErrors = false;
results.forEach((result: SnippetCompilationResult) => {
if (result.error) {
hasErrors = true;
console.error(
`\nError compiling example code block ${result.index} in file ${result.file}:`
);
console.error(result.error.message);
console.error('Original code:');
console.error(result.snippet);
}
});
if (hasErrors) {
process.exit(1);
} else {
console.log('All TypeScript snippets compiled successfully!');
}
})
.catch((error: unknown) => {
console.error('An unexpected error occurred during snippet compilation:', error);
process.exit(1);
});