TypeScript API Extractor

1.0.0-beta.3 · active · verified Sun Apr 19

typescript-api-extractor is a utility designed to analyze TypeScript source code and generate structured metadata about its exported API. It leverages the TypeScript Compiler API to extract information on functions, React components, interfaces, types, enums, and their associated JSDoc comments. The package is currently in beta (v1.0.0-beta.3 as of the last release), indicating active development with potential for API changes, though it appears to have a consistent release cadence for bug fixes and new features. Its key differentiators include specialized React component analysis, robust JSDoc parsing, intelligent reference resolution for complex types, and the ability to selectively parse files or entire projects, outputting a detailed `ModuleNode` structure for programmatic consumption. This tool is particularly useful for generating documentation, schema definitions, or other forms of API introspection directly from TypeScript codebases.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to load a `tsconfig.json`, create a TypeScript `Program` instance, and then iterate through all project files to extract and log their API definitions using `parseFromProgram`.

import ts from 'typescript';
import { loadConfig, parseFromProgram, type ModuleNode } from 'typescript-api-extractor';

async function extractApi() {
  // Specify the path to your tsconfig.json file
  const tsConfigPath = './tsconfig.json';

  // Load TypeScript configuration
  let config;
  try {
    config = loadConfig(tsConfigPath);
  } catch (error) {
    console.error(`Failed to load tsconfig from ${tsConfigPath}:`, error);
    return;
  }

  // Create a TypeScript program based on the loaded configuration
  const program = ts.createProgram(config.fileNames, config.options);

  console.log('Starting API extraction...');

  // Parse all files in the project referenced by tsconfig.json
  for (const file of config.fileNames) {
    try {
      const moduleInfo: ModuleNode = parseFromProgram(file, program);
      console.log(`\nExtracted API from ${file}:`);
      // Log a simplified version or specific parts to avoid excessive output
      console.log(`  Module Name: ${moduleInfo.name}`);
      console.log(`  Exported Symbols: ${moduleInfo.exports.map(e => e.name).join(', ')}`);
      // For full details, you would inspect moduleInfo more deeply
    } catch (error) {
      console.error(`Failed to parse ${file}:`, error);
    }
  }
  console.log('\nAPI extraction complete.');
}

extractApi().catch(console.error);

view raw JSON →