Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
0a4f4a5
add dev dependencies for tests and test coverage via jest
Winner95 Nov 20, 2025
76d56f2
add some files to git ignore and test npm package
Winner95 Nov 20, 2025
a057550
add tests and test coverage
Winner95 Nov 20, 2025
3289e2b
add correct test to expected thrown error
Winner95 Nov 20, 2025
84fa158
Improve React types
Winner95 Nov 20, 2025
d9710ec
Add reusable types
Winner95 Nov 20, 2025
6b4051a
add types for jest
Winner95 Nov 20, 2025
0b2c805
add no props case for AccessibleButton
Winner95 Nov 20, 2025
71af097
Currently returns error for DataTableManager
Winner95 Nov 20, 2025
9b4d29e
change naming for ExportedInterfacePropsComponent
Winner95 Nov 20, 2025
09fbe6c
add test coverage for union type props
Winner95 Nov 20, 2025
e065e4d
rename InterfacePropsComponent
Winner95 Nov 20, 2025
f42e887
add complex interfaces
Winner95 Nov 20, 2025
e55e42b
add test coverage for any type
Winner95 Nov 20, 2025
707ac00
NoInfer type type coverage
Winner95 Nov 20, 2025
7ae9cc6
add test for React.ComponentPropsWithoutRef<Props>
Winner95 Nov 20, 2025
2b97121
add test coverage for exception for SecondaryIconButtonProps
Winner95 Nov 20, 2025
c3726c1
add cover for a generic type parameter with a default
Winner95 Nov 20, 2025
84292f6
add coverage for several exports in one component
Winner95 Nov 20, 2025
77095c9
add cover for React children component props
Winner95 Nov 20, 2025
487d780
add test cover for Tag component
Winner95 Nov 20, 2025
713c392
add more external types
Winner95 Nov 20, 2025
28f80b3
add test for imported types
Winner95 Nov 20, 2025
6ee740d
add test cover for dynamic index types
Winner95 Nov 20, 2025
1e93a80
add disclaimer
Winner95 Nov 25, 2025
8c687ea
npm publishing - avoid default tag publishing strategy
Winner95 Nov 25, 2025
8a74131
add more Support & responsibility section
Winner95 Nov 25, 2025
db9fa87
update Readme for 2025
Winner95 Nov 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
coverage
node_modules
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# avoid using the latest tag by default
tag=next
252 changes: 231 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,258 @@
# Typescript-react-function-component-props-handler [![npm version](https://badge.fury.io/js/typescript-react-function-component-props-handler.svg)](https://badge.fury.io/js/typescript-react-function-component-props-handler)
# typescript-react-function-component-props-handler

Custom handler to proccess react function component props for `react-docgen` package. It allows you to parse props, which are typed as React.functionComponent&lt;props> without rewriting your code.
[![npm version](https://badge.fury.io/js/typescript-react-function-component-props-handler.svg)](https://badge.fury.io/js/typescript-react-function-component-props-handler)
[![npm downloads](https://img.shields.io/npm/dm/typescript-react-function-component-props-handler.svg)](https://www.npmjs.com/package/typescript-react-function-component-props-handler)
[![license](https://img.shields.io/npm/l/typescript-react-function-component-props-handler.svg)](./LICENSE)
<!-- Enable this once CI is set up:
[![CI](https://github.com/Winner95/typescript-react-function-component-props-handler/actions/workflows/ci.yml/badge.svg)](https://github.com/Winner95/typescript-react-function-component-props-handler/actions/workflows/ci.yml)
-->

## Intro
Custom handler to **process React function component props** for [`react-docgen`](https://react-docgen.dev/).
It lets you parse props for components typed as `React.FC<Props>` / `React.FunctionComponent<Props>` **without** rewriting your code.

React-docgen 5.3.0 allow you to parse React.components and generate documentation. But if you don't explicitly set type of pros parameter, information about props is not visible in output. This custom handler allows you to fix [this problem](https://github.com/reactjs/react-docgen/issues/387). This is one-purpose, zero-dependency project.
---

## Getting started
## Why this project exists

### Add nececcary dependencies to your project
`react-docgen` can parse React components and generate JSON documentation, but for TypeScript projects there’s a common gotcha:

```tsx
// Button.tsx
import React from "react";

export type ButtonProps = {
/** Text inside the button */
label: string;
/** Whether the button is disabled */
disabled?: boolean;
};

export const Button: React.FC<ButtonProps> = (props) => (
<button disabled={props.disabled}>{props.label}</button>
);
```

With `react-docgen@5.x`:

- If you **don’t** explicitly type the `props` parameter
(e.g. `({ label }: ButtonProps)`),
- And instead only type the component as `React.FC<ButtonProps>`,

then the props information is usually **missing** in the generated docs.

This handler:

- Looks at the component’s **function type** (`React.FC<ButtonProps>`)
- Copies that type onto the **props parameter** in the AST
- So `react-docgen` can finally “see” your props and emit them in the output.

It’s a small, one-purpose, zero-dependency helper to fix that specific gap.

---

## Compatibility

| Library version | `react-docgen` | Node |
| ------------------------- | -------------- | ------ |
| `1.x` (current) | `5.x` | `>=14` |
| `2.x` (planned / WIP)`*` | `6.x` | `>=18` |

`*` The roadmap for `2.x` (react-docgen 6 support) is tracked in the repo issues.

---

## Installation

Using **yarn**:

```bash
yarn add typescript-react-function-component-props-handler react-docgen
```

### Write script for running react-docgen
Using **npm**:

```bash
npm install typescript-react-function-component-props-handler react-docgen
```

---

## Quick start (step-by-step)

### 1. Add a component typed as `React.FC`

```tsx
// src/Button.tsx
import React from "react";

export type ButtonProps = {
/** Text inside the button */
label: string;
/** Whether the button is disabled */
disabled?: boolean;
};

export const Button: React.FC<ButtonProps> = ({ label, disabled }) => (
<button disabled={disabled}>{label}</button>
);
```

Add the following content to the file:
Note: The parameter itself is **not** explicitly typed, only the component is.

---

### 2. Create a small docgen script

```js
// docgen.js
const fs = require("fs");
const path = require("path");
const reactDocs = require("react-docgen");

// The custom handler from this package
const setParamsTypeDefinitionFromFunctionType =
require("typescript-react-function-component-props-handler");

const reactDocs = require('react-docgen');
const filePath = process.argv[2];
if (!filePath) {
console.error("Usage: node docgen.js <path-to-component>");
process.exit(1);
}

const setParamsTypeDefinitionFromFunctionType = require('typescript-react-function-component-props-handler');
const source = fs.readFileSync(path.resolve(filePath), "utf8");

const doc = reactDocs.parse(
source,
null,
[setParamsTypeDefinitionFromFunctionType, ...reactDocs.defaultHandlers],
null
source,
null,
[setParamsTypeDefinitionFromFunctionType, ...reactDocs.defaultHandlers],
null
);

console.log(JSON.stringify(doc, null, 2));
```

**Note:** `source` is a path to your component to parse.
---

### 3. Run the script

```bash
node docgen.js src/Button.tsx
```

You should now see your props in the JSON output, even though `props` wasn’t explicitly typed on the parameter.

---

### 4. Example output (trimmed)

```json
{
"displayName": "Button",
"props": {
"label": {
"type": { "name": "string" },
"required": true,
"description": "Text inside the button"
},
"disabled": {
"type": { "name": "boolean" },
"required": false,
"description": "Whether the button is disabled"
}
}
}
```

---

## Usage options

### 1. Programmatic usage (Node script)

This is the core way to use the handler:

```js
const reactDocs = require("react-docgen");
const setParamsTypeDefinitionFromFunctionType =
require("typescript-react-function-component-props-handler");

const doc = reactDocs.parse(
source, // string with your component source
null, // resolver (null -> default)
[setParamsTypeDefinitionFromFunctionType, ...reactDocs.defaultHandlers],
null
);
```

> `source` should be the component source code (e.g. read from a `.tsx` file).

---

## What code examples are covered

The handler is focused on **function components typed via React’s function component types**, for example:

- `const Button: React.FC<ButtonProps> = (props) => { … }`
- `const Button: React.FunctionComponent<ButtonProps> = (props) => { … }`
- Both named and default exports
- Components where props are **not** explicitly typed at the parameter level

Things this handler **does not** try to solve:

- Class components (already handled by `react-docgen`)
- Function components where the parameter is already explicitly typed
- Non-TypeScript code

If you hit a pattern that doesn’t work as expected, feel free to open an issue with a minimal repro.

---

## Disclaimer

This library is provided **“as is”**, without warranty of any kind.
Use it at your own risk. The author(s) are **not responsible** for any issues, data loss, or production incidents resulting from its use.

This package is a small helper for `react-docgen`.
It is **not** an officially supported product of any company I work or have worked for.

---

## Support & responsibility

This is a best-effort open source project maintained in personal spare time.

- I’ll try to review issues and PRs.
- I can’t guarantee response times, feature work, or fixes for every edge case.
- Please evaluate the library and decide if it’s suitable for your project before using it in production.

Contributions (bug reports, failing test cases, docs improvements) are very welcome.

---

## Live examples

These sandboxes show concrete configurations with and without this handler.

- **react-docgen@6 – config without this handler (props NOT shown)**
https://codesandbox.io/p/sandbox/initial-600-beta22-config-prototype-with-not-showing-props-react-docgen-kwz72

- **react-docgen@6 – config with this handler (props shown)**
https://codesandbox.io/p/sandbox/initial-600-beta22-config-working-prototype-with-showing-props-react-docgen-ftvf2

> For the StackBlitz examples below, open the built-in terminal and run `node index.js`.

- **StackBlitz demo #1 (react-docgen setup, baseline)**
https://stackblitz.com/edit/stackblitz-starters-ry91wvpz?file=index.js

## What code-examples are covered
- **StackBlitz demo #2 (variant with this handler wired in)**
https://stackblitz.com/edit/stackblitz-starters-2w1zpcnx?file=index.js

Most of general and corner-cases for `React.functionComponents` were covered.
---

## Useful links

You can test your components in these online services:
You can explore and debug components & ASTs with:

* [React-docgen: playground](https://reactcommunity.org/react-docgen/)
* [TypeScript AST Viewer](https://ts-ast-viewer.com/)
* [AST Explorer](https://astexplorer.net/)
- [`react-docgen` playground](https://react-docgen.dev/playground)
- [TypeScript AST Viewer](https://ts-ast-viewer.com/)
- [AST Explorer](https://astexplorer.net/)
Loading