CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-docgen-typescript

A simple parser for React properties defined in TypeScript instead of propTypes

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

configuration.mddocs/

Parser Configuration

Advanced parser configuration for customizing TypeScript compiler options and creating reusable parser instances with specific configurations.

Capabilities

Default Configuration Parser

Creates a parser with default TypeScript configuration and custom parsing options.

/**
 * Constructs a parser for a default configuration
 * @param parserOpts - Parser options for customizing behavior
 * @returns FileParser instance with default TypeScript config
 */
function withDefaultConfig(parserOpts: ParserOptions = defaultParserOpts): FileParser;

Usage Examples:

import { withDefaultConfig } from "react-docgen-typescript";

// Create parser with custom options
const parser = withDefaultConfig({
  propFilter: {
    skipPropsWithName: ['className', 'style'],
    skipPropsWithoutDoc: true
  },
  shouldExtractLiteralValuesFromEnum: true
});

// Use the parser multiple times
const buttonDocs = parser.parse("./src/Button.tsx");
const inputDocs = parser.parse("./src/Input.tsx");

Custom TypeScript Configuration Parser

Creates a parser using a specific tsconfig.json file for TypeScript compilation.

/**
 * Constructs a parser for a specified tsconfig file
 * @param tsconfigPath - Path to tsconfig.json file
 * @param parserOpts - Parser options for customizing behavior
 * @returns FileParser instance with custom TypeScript config
 * @throws Error when tsconfig cannot be loaded or parsed
 */
function withCustomConfig(
  tsconfigPath: string,
  parserOpts: ParserOptions
): FileParser;

Usage Examples:

import { withCustomConfig } from "react-docgen-typescript";

// Use project's tsconfig.json
const parser = withCustomConfig("./tsconfig.json", {
  savePropValueAsString: true
});

// Use specific tsconfig for documentation
const parser = withCustomConfig("./tsconfig.docs.json", {
  shouldExtractValuesFromUnion: true,
  componentNameResolver: (exp, source) => {
    // Custom component name resolution logic
    return exp.getName() === "default" ? "MyComponent" : exp.getName();
  }
});

const docs = parser.parse("./src/components/*.tsx");

Compiler Options Parser

Creates a parser with explicit TypeScript compiler options.

/**
 * Constructs a parser for a specified set of TS compiler options
 * @param compilerOptions - TypeScript compiler options
 * @param parserOpts - Parser options for customizing behavior
 * @returns FileParser instance with specified compiler options
 */
function withCompilerOptions(
  compilerOptions: ts.CompilerOptions,
  parserOpts: ParserOptions = defaultParserOpts
): FileParser;

Usage Examples:

import { withCompilerOptions } from "react-docgen-typescript";
import * as ts from "typescript";

// Custom compiler options
const parser = withCompilerOptions(
  {
    jsx: ts.JsxEmit.ReactJSX,
    target: ts.ScriptTarget.ES2020,
    moduleResolution: ts.ModuleResolutionKind.NodeJs,
    strict: true,
    esModuleInterop: true
  },
  {
    shouldExtractLiteralValuesFromEnum: true,
    shouldSortUnions: true
  }
);

const docs = parser.parse("./src/ModernComponent.tsx");

FileParser Interface

The parser interface returned by configuration functions.

interface FileParser {
  /**
   * Parse component files and return documentation
   * @param filePathOrPaths - Single file path or array of paths
   * @returns Array of component documentation
   */
  parse(filePathOrPaths: string | string[]): ComponentDoc[];
  
  /**
   * Parse with custom TypeScript program provider
   * @param filePathOrPaths - Single file path or array of paths  
   * @param programProvider - Function that returns TypeScript Program
   * @returns Array of component documentation
   */
  parseWithProgramProvider(
    filePathOrPaths: string | string[],
    programProvider?: () => ts.Program
  ): ComponentDoc[];
}

Advanced Configuration Patterns

Shared Parser Instance

import { withDefaultConfig } from "react-docgen-typescript";

// Create shared parser for consistent configuration
const sharedParser = withDefaultConfig({
  propFilter: {
    skipPropsWithName: ['className', 'style', 'children'],
    skipPropsWithoutDoc: true
  },
  shouldExtractLiteralValuesFromEnum: true,
  savePropValueAsString: true
});

// Use across multiple parsing operations
const componentDocs = [
  ...sharedParser.parse("./src/components/Button.tsx"),
  ...sharedParser.parse("./src/components/Input.tsx"),
  ...sharedParser.parse("./src/components/Modal.tsx")
];

TypeScript Program Reuse

import { withCompilerOptions } from "react-docgen-typescript";
import * as ts from "typescript";

const compilerOptions = {
  jsx: ts.JsxEmit.React,
  target: ts.ScriptTarget.ES2019,
  moduleResolution: ts.ModuleResolutionKind.NodeJs
};

const parser = withCompilerOptions(compilerOptions);

// Create TypeScript program once for multiple parses
const filePaths = ["./src/Button.tsx", "./src/Input.tsx"];
const program = ts.createProgram(filePaths, compilerOptions);

const docs = parser.parseWithProgramProvider(filePaths, () => program);

Error Handling

Configuration functions can throw errors in several scenarios:

import { withCustomConfig } from "react-docgen-typescript";

try {
  const parser = withCustomConfig("./invalid-tsconfig.json", {});
} catch (error) {
  // Handle configuration errors
  if (error.message.includes("Cannot load custom tsconfig.json")) {
    console.error("Invalid tsconfig path:", error.message);
  }
}

Common error scenarios:

  • Invalid tsconfig.json path: File doesn't exist or isn't readable
  • Malformed tsconfig.json: Invalid JSON or TypeScript configuration
  • TypeScript compilation errors: Invalid compiler options or project setup

Install with Tessl CLI

npx tessl i tessl/npm-react-docgen-typescript

docs

advanced-parser.md

component-docs.md

configuration.md

index.md

parser-options.md

parsing.md

tile.json