TypeScript Documentation Verifier

3.0.2 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This code snippet demonstrates programmatic usage of the `typescript-docs-verifier` library. It processes and compiles TypeScript code blocks within specified Markdown files using a dedicated `tsconfig.json`, reporting any compilation errors encountered.

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);
  });

view raw JSON →