To parse a file for docgen information use the function.
parse
const docgen = require("react-docgen-typescript");const options = { savePropValueAsString: true,};// Parse a file for docgen infodocgen.parse("./path/to/component", options);
If you want to customize the typescript configuration or docgen options, this package exports a variety of ways to create custom parsers.
const docgen = require("react-docgen-typescript");// Create a parser with the default typescript config and custom docgen optionsconst customParser = docgen.withDefaultConfig(options);const docs = customParser.parse("./path/to/component");// Create a parser with the custom typescript and custom docgen optionsconst customCompilerOptionsParser = docgen.withCompilerOptions( { esModuleInterop: true }, options);// Create a parser with using your typescript configconst tsConfigParser = docgen.withCustomConfig("./tsconfig.json", { savePropValueAsString: true,});
React Styleguidist integration
Include following line in your styleguide.config.js:
If a string is returned, then the component will use that name. Else it will fallback to the default logic of parser.
shouldExtractLiteralValuesFromEnum: boolean
If set to true, string enums and unions will be converted to docgen enum format. Useful if you use Storybook and want to generate knobs automatically using addon-smart-knobs.
shouldExtractValuesFromUnion: boolean
If set to true, every unions will be converted to docgen enum format.
shouldSortUnions: boolean
When used in combination with shouldExtractValuesFromUnion or shouldExtractLiteralValuesFromEnum, sorts union members in string-sort order when set to true. This is useful for ensuring the same order of members every time.
The typescript is pretty complex and there are many different ways how
to define components and their props so it's realy hard to support all
these use cases. That means only one thing, contributions are highly
welcome. Just keep in mind that each PR should also include tests for
the part it's fixing.
Thanks to all contributors without their help there wouldn't be a single
bug fixed or feature implemented. Check the contributors tab to find out
more. All those people supported this project. THANK YOU!
Thanks to others
The integration with React Styleguidist wouldn't be possible without Vyacheslav Slinko pull request #118 at React Styleguidist.