react-to-typescript-definitions is a development tool that generates TypeScript declaration files (`.d.ts`) directly from React component source code. It analyzes React components, their PropTypes (including `any`, `array`, `bool`, `func`, `number`, `object`, `string`, `node`, `element`, `oneOfType`, `arrayOf`, `symbol`, `shape`), and JSDoc comments to infer their type definitions. The library supports both ES6/ES7 class components and can handle modern JavaScript features like optional chaining. It offers both a command-line interface (CLI) for direct file processing and an API for programmatic use, allowing integration into build pipelines. The current stable version is 3.1.1, released in April 2021, and its development appears to be in a maintenance phase. It provides a crucial bridge for projects transitioning from JavaScript with PropTypes to TypeScript, automatically creating type information for existing component libraries, though its reliance on PropTypes ties it to an older React pattern.
npm install react-to-typescript-definitionsVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use the `generateFromSource` API to create TypeScript definitions from a React component string with PropTypes.
Upgrade your Node.js environment to version 10 or higher. The current stable Node.js LTS is recommended.
Pipe the file content to the CLI, for example: `cat myComponent.jsx | react2dts --module-name MyComponent`.
Pass an `instanceOfResolver` function in the options object to `generateFromFile`, `generateFromSource`, or `generateFromAst` that resolves type names to their file paths.
Consider migrating your React components directly to TypeScript for better developer experience and type safety, reducing the need for an intermediate PropTypes-to-TypeScript step.
Upgrade your Node.js environment. Install Node.js v10 or a newer LTS version.
Use a command like `cat yourComponent.jsx | react2dts` to pipe the file content to the CLI. The CLI doesn't accept file paths as direct arguments.
Ensure you are using named imports with ESM syntax: `import { generateFromSource } from 'react-to-typescript-definitions';`No dependency data recorded yet.