babel-plugin-typescript-to-proptypes is a Babel plugin designed to automatically generate React PropTypes from TypeScript interfaces and type aliases within your React components. This tool is particularly useful for projects that want to leverage TypeScript for type safety during development while still providing runtime PropTypes for environments that rely on them (e.g., older React versions, certain testing setups, or for debugging purposes). The current stable version is 2.1.0, and it maintains an active release cadence with frequent updates for Babel and TypeScript compatibility. A key differentiator is that it operates solely on the Babel AST, meaning it does not run TypeScript's type checker and therefore cannot resolve type information referenced from external files. It integrates directly into your Babel build pipeline.
npm install babel-plugin-typescript-to-proptypesVerified import paths — ran on the pinned version, not inferred.
Demonstrates a simple React functional component with a TypeScript interface, showing the input code that the plugin transforms into code with `PropTypes`. The Babel configuration snippet shows how to enable the plugin, typically outside of production builds.
Upgrade TypeScript to v4.0.0 or v5.0.0 and Node.js to v12.17.0 or higher. Alternatively, pin your dependency to `babel-plugin-typescript-to-proptypes@^1`.
Ensure all TypeScript interfaces and type aliases used for component props are declared directly within the same `.tsx` or `.ts` file as the React component itself. Avoid importing prop types from other modules if you expect them to be converted.
Include `@babel/preset-react` in your Babel presets array. If you are targeting older JavaScript environments, also add `@babel/plugin-proposal-class-properties` to your plugins.
Wrap the plugin inclusion in your Babel config with an environment check, e.g., `process.env.NODE_ENV !== 'production' && 'babel-plugin-typescript-to-proptypes'`.
Ensure `babel-plugin-typescript-to-proptypes` is installed as a dev dependency (`npm install --save-dev babel-plugin-typescript-to-proptypes` or `yarn add --dev babel-plugin-typescript-to-proptypes`) and the name is correctly spelled in `babel.config.js` or `.babelrc`.
Ensure that all props marked as required in your TypeScript interface are always provided with a valid value when `MyComponent` is used. This is a logic error in your React component usage, not necessarily a plugin error.
Make sure `prop-types` is installed (`npm install prop-types` or `yarn add prop-types`) and that `import PropTypes from 'prop-types';` (or `const PropTypes = require('prop-types');`) is present in the file that consumes the generated PropTypes. The plugin *adds* the `import` statement for you, but the package still needs to be installed.