Babel Plugin: TypeScript to PropTypes
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.
Common errors
-
Error: Plugin 'babel-plugin-typescript-to-proptypes' not found
cause The plugin is not correctly installed or referenced in the Babel configuration.fixEnsure `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`. -
Warning: Failed prop type: The prop `myProp` is marked as required in `MyComponent`, but its value is `undefined`.
cause This is a runtime warning from `prop-types` itself, indicating that a required prop was not provided or was `undefined` at runtime after the plugin has generated the PropTypes.fixEnsure 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. -
ReferenceError: PropTypes is not defined
cause The generated PropTypes code relies on the `prop-types` package, but it's not imported or available in the runtime environment.fixMake 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.
Warnings
- breaking Version 2.0.0 dropped support for TypeScript v3 and Node.js v10. Users on these older environments must upgrade their toolchain or remain on `babel-plugin-typescript-to-proptypes` v1.x.
- gotcha This plugin operates on Babel's Abstract Syntax Tree (AST) and does NOT run TypeScript's full type checker. This means it cannot resolve type information for props or types referenced from external files. All relevant type definitions must be within the same file as the component.
- gotcha Requires either `@babel/plugin-syntax-jsx` or `@babel/preset-react` to parse JSX syntax. If transpiling to ES5 or lower, `@babel/plugin-proposal-class-properties` is also required.
- gotcha It is generally recommended to enable this plugin only for development environments or when building a library, not for production application builds, as PropTypes add to bundle size and are typically not needed in production.
Install
-
npm install babel-plugin-typescript-to-proptypes -
yarn add babel-plugin-typescript-to-proptypes -
pnpm add babel-plugin-typescript-to-proptypes
Imports
- babel-plugin-typescript-to-proptypes
plugins.push('babel-plugin-typescript-to-proptypes'); - babel-plugin-typescript-to-proptypes options
plugins.push(['babel-plugin-typescript-to-proptypes', { comments: true }]); - PropTypes
const PropTypes = require('prop-types');import PropTypes from 'prop-types';
Quickstart
import React from 'react';
import PropTypes from 'prop-types';
interface MyComponentProps {
/** A required string prop */
name: string;
count?: number;
isActive: boolean;
}
function MyComponent(props: MyComponentProps) {
return (
<div>
<p>Hello, {props.name}</p>
{props.count && <p>Count: {props.count}</p>}
{props.isActive ? <p>Active</p> : <p>Inactive</p>}
</div>
);
}
// babel.config.js (config for the plugin)
// This quickstart demonstrates the *before* and *after* for the plugin.
// To actually run the plugin, you'd need a Babel setup.
// Add this to your Babel configuration's plugins array:
// plugins: [
// '@babel/preset-typescript',
// '@babel/preset-react',
// process.env.NODE_ENV !== 'production' && ['babel-plugin-typescript-to-proptypes', { comments: true }],
// ].filter(Boolean),
// Expected output after plugin execution (simplified):
// MyComponent.propTypes = {
// /** A required string prop */
// name: PropTypes.string.isRequired,
// count: PropTypes.number,
// isActive: PropTypes.bool.isRequired,
// };
export default MyComponent;