A simple parser for React properties defined in TypeScript instead of propTypes
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Simple one-line parsing functionality for extracting React component documentation with default TypeScript configurations.
Parses React component files with default TypeScript options and returns component documentation.
/**
* Parses a file with default TS options
* @param filePathOrPaths - Component file path(s) that should be parsed
* @param parserOpts - Options used to parse the files
* @returns Array of component documentation objects
*/
function parse(
filePathOrPaths: string | string[],
parserOpts: ParserOptions = defaultParserOpts
): ComponentDoc[];Usage Examples:
import { parse } from "react-docgen-typescript";
// Parse a single file
const docs = parse("./src/Button.tsx");
// Parse multiple files
const docs = parse([
"./src/Button.tsx",
"./src/Input.tsx",
"./src/Modal.tsx"
]);
// Parse with options
const docs = parse("./src/Button.tsx", {
savePropValueAsString: true,
shouldExtractLiteralValuesFromEnum: true,
propFilter: {
skipPropsWithName: ['className', 'style']
}
});The default parser options used when none are specified.
const defaultParserOpts: ParserOptions;The default TypeScript compiler options used by the parse function.
const defaultOptions: ts.CompilerOptions;The default options include:
{
jsx: ts.JsxEmit.React,
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.Latest,
esModuleInterop: true
}The parser automatically detects various types of React components:
The parse function will throw errors in the following cases:
import { parse } from "react-docgen-typescript";
try {
const docs = parse("./src/Component.tsx");
console.log(docs);
} catch (error) {
console.error("Parsing failed:", error.message);
}Install with Tessl CLI
npx tessl i tessl/npm-react-docgen-typescript