Registry / devops / react-docgen-typescript

react-docgen-typescript

JSON →
library2.4.0jsnpmunverified

This library, `react-docgen-typescript`, provides a robust parser for extracting documentation information from React components written in TypeScript. It analyzes TypeScript files to generate documentation data, similar to how `react-docgen` works for JavaScript components with `propTypes`. The current stable version is 2.4.0, with releases occurring semi-frequently, indicating active maintenance and feature development based on recent changelogs. Its primary differentiator is its native support for TypeScript, allowing it to correctly interpret complex type definitions, interfaces, and generics to generate detailed prop tables, including descriptions, types, and default values. It is often used in conjunction with tools like React Styleguidist to automatically generate component documentation.

npm install react-docgen-typescript
INSTALL
IMPORT
SIG · REACT-DOCGEN-TYPES
R
react-docgen-typescript
devopsjavascriptv2.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
import { parse } from 'react-docgen-typescript'
const docgen = require('react-docgen-typescript'); docgen.parse('./path/to/component')
'parse' is the core function for processing a single file. For CommonJS, the common pattern shown in the README is to import the entire module and then access 'parse' from the imported object.
withDefaultConfig
import { withDefaultConfig } from 'react-docgen-typescript'
const docgen = require('react-docgen-typescript'); docgen.withDefaultConfig(options)
Used to create a parser instance with default TypeScript compiler options and custom docgen options.
withCustomConfig
import { withCustomConfig } from 'react-docgen-typescript'
const docgen = require('react-docgen-typescript'); docgen.withCustomConfig('./tsconfig.json', options)
Allows creating a parser using an existing tsconfig.json file for TypeScript configuration, which is recommended for most projects.
ComponentDoc
import type { ComponentDoc } from 'react-docgen-typescript'
This is a TypeScript type definition for the structured output of the parser for a single component. It should be imported as a type.

This example demonstrates how to parse a TypeScript React component using `withCustomConfig` to specify a `tsconfig.json` and custom docgen options, then prints the generated documentation to the console.

import { withCustomConfig } from 'react-docgen-typescript'; import * as fs from 'fs'; import * as path from 'path'; // Define a dummy component file for demonstration const componentCode = ` import React from 'react'; interface ButtonProps { /** * The text content of the button. */ label: string; /** * Is this the principal call to action on the page? * @default false */ primary?: boolean; /** * What background color to use */ backgroundColor?: string; /** * How large should the button be? * @default 'medium' */ size?: 'small' | 'medium' | 'large'; /** * Optional click handler */ onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void; } /** * A primary button component */ export const Button: React.FC<ButtonProps> = ({ label, primary = false, backgroundColor, size = 'medium', onClick, }) => { const mode = primary ? 'storybook-button--primary' : 'storybook-button--secondary'; return ( <button type="button" className={['storybook-button', `storybook-button--${size}`, mode].join(' ')} style={{ backgroundColor }} onClick={onClick} > {label} </button> ); }; `; const tempDir = path.join(__dirname, 'temp'); if (!fs.existsSync(tempDir)) { fs.mkdirSync(tempDir); } const tempComponentPath = path.join(tempDir, 'Button.tsx'); fs.writeFileSync(tempComponentPath, componentCode); // Create a basic tsconfig.json for the parser const tsconfigPath = path.join(tempDir, 'tsconfig.json'); fs.writeFileSync(tsconfigPath, JSON.stringify({ compilerOptions: { jsx: "react", module: "ESNext", target: "ESNext", esModuleInterop: true, strict: true, skipLibCheck: true, }, include: [tempDir + "/**/*"] }, null, 2)); const parser = withCustomConfig(tsconfigPath, { shouldExtractLiteralValuesFromEnum: true, propFilter: { skipPropsWithoutDoc: true, }, }); try { const componentDocs = parser.parse(tempComponentPath); console.log(JSON.stringify(componentDocs, null, 2)); } catch (error) { console.error("Error parsing component:", error); } finally { // Clean up temporary files fs.unlinkSync(tempComponentPath); fs.unlinkSync(tsconfigPath); fs.rmdirSync(tempDir); }
Debug
Known issues
breakingVersion 2.0.0 introduced a breaking change by requiring TypeScript 4.3 or newer due to the use of a new TypeScript API function. Projects using older TypeScript versions will encounter errors.
fix
Upgrade your project's 'typescript' dependency to version 4.3.x or higher to meet the minimum requirement.
affects: >=2.0.0
breakingStarting from version 2.4.0, the 'esModuleInterop' compiler option is set as a default configuration during parsing when using `parse` or `withDefaultConfig`. If your project relies on specific 'esModuleInterop' behavior that conflicts with this default (e.g., explicit `false`), you might experience changes in how imports are resolved during parsing.
fix
If conflicts or unexpected behavior arise, explicitly configure 'esModuleInterop' via `withCompilerOptions` or `withCustomConfig` to match your project's `tsconfig.json` settings.
affects: >=2.4.0
gotchaThe library depends on 'typescript' as a peer dependency. Incompatible 'typescript' versions (e.g., using an older version than '>= 4.3.x' with `react-docgen-typescript` v2.0.0+) can lead to parsing errors or unexpected behavior during documentation generation.
fix
Always ensure your project's 'typescript' dependency meets the peer dependency constraint specified in `react-docgen-typescript`'s `package.json`. Use `npm install typescript@latest` or a specific compatible version to resolve potential conflicts.
affects: *
Errors
Common errors & fixes
TypeError: (tag.text || "").trim is not a function
This error typically occurred in versions prior to 2.0.0 due to an issue with how JSDoc tag text was processed, where the 'text' property could be null or undefined.
fix
Upgrade 'react-docgen-typescript' to version 2.0.0 or newer to resolve this bug, which included fixes for tag text handling.
Cannot read property 'length' of undefined
This often indicates an issue in older versions (pre-2.0.0) where internal data structures for declarations or other parsed elements were unexpectedly undefined, leading to runtime errors when trying to access properties like 'length'.
fix
Upgrade 'react-docgen-typescript' to version 2.0.0 or newer, as this was addressed in fixes for that major release.
Property 'declarations' of 'PropItem' is undefined or has no 'length' property
In older versions, particularly around 1.20.5, specific scenarios could lead to 'declarations' being undefined, causing runtime errors when attempting to access its 'length' property.
fix
Ensure you are using 'react-docgen-typescript' version 1.20.5 or newer, or ideally any 2.x version, where these edge cases were resolved.
trimFileName no longer works in a monorepo
This was a specific bug in versions prior to 2.2.2 where the 'trimFileName' logic failed to correctly process file paths when used within a monorepo setup.
fix
Upgrade 'react-docgen-typescript' to version 2.2.2 or higher to fix the 'trimFileName' functionality in monorepos.
Upgrade
Version history
2.4.0latest on npm
Audit
Dependencies
typescriptrequiredRequired peer dependency for parsing TypeScript files.
Agent activity
6 hits · last 30 days
node
6
Resources
react-docgen-typescript — npm install react-docgen-typescript · libregistry