React PropType Utilities
prop-types-extra is a utility library designed to augment React's prop-validation capabilities by providing an extended set of PropTypes. It builds upon the foundational `prop-types` package, offering specialized validators that address common and complex validation scenarios in React component development. Key validators include `all`, which enables combining multiple validation rules; `componentOrElement`, for validating props expecting either a React component or a DOM element reference; `deprecated`, a utility to log deprecation warnings for specific props; `elementType`, for ensuring a prop is a valid React element type (e.g., a string for a DOM element or a component constructor); and `isRequiredForA11y`, which enforces accessibility requirements. The package is currently at version 1.1.1. As a supplementary tool for `prop-types`, its release cadence is typically stable, with updates driven primarily by React's evolution or specific bug fixes rather than frequent feature additions. It differentiates itself by providing ready-to-use, advanced validation patterns, thereby simplifying the creation of robust and semantically correct React components.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'elementType')
cause Incorrect import statement for `elementType` or attempting to use CommonJS `require` syntax when the environment expects ESM.fixEnsure you are using `import { elementType } from 'prop-types-extra';` for named exports or `import elementType from 'prop-types-extra/lib/elementType';` for direct path imports. Verify your build system correctly handles ESM modules. -
TypeError: PropTypes.bool is not a function
cause The base `PropTypes` object is not correctly imported or available. `prop-types-extra` provides *additional* validators, but expects the core `PropTypes` object from the `prop-types` package to be present.fixAdd `import PropTypes from 'prop-types';` at the top of your file. Ensure the `prop-types` package is installed (`npm install prop-types`). -
Warning: Failed prop type: The prop `block` requires `vertical` to be set to have any effect in `MyComponent`.
cause This is an expected prop type validation failure due to the component's usage violating a custom validation rule defined using `prop-types-extra`'s `all` validator.fixAdjust the component's props to satisfy the validation rules, e.g., ensure `vertical` is `true` when `block` is `true` as per the `all` validator's custom rule.
Warnings
- gotcha Importing `prop-types-extra` directly imports all validators, which can increase bundle size.
- gotcha This library extends the standard `prop-types` library and does not replace it. You still need to import `PropTypes` from `prop-types` (or `React.PropTypes` in very old React versions) for basic validations like `PropTypes.string`.
- gotcha This package is designed for environments where `PropTypes` has been extracted into the standalone `prop-types` package (React 15.5.0 and later). Using it with significantly older React versions (pre-15.5.0) might lead to incompatibility or `PropTypes` not being correctly resolved.
Install
-
npm install prop-types-extra -
yarn add prop-types-extra -
pnpm add prop-types-extra
Imports
- elementType
const { elementType } = require('prop-types-extra');import { elementType } from 'prop-types-extra'; - elementType (direct path)
import { elementType } from 'prop-types-extra/lib/elementType';import elementType from 'prop-types-extra/lib/elementType';
- all
const all = require('prop-types-extra').all;import { all } from 'prop-types-extra'; - deprecated
import deprecated from 'prop-types-extra/lib/deprecated';
import { deprecated } from 'prop-types-extra';
Quickstart
import React from 'react';
import PropTypes from 'prop-types';
import { all } from 'prop-types-extra';
interface MyComponentProps {
vertical: boolean;
block: boolean;
}
const MyComponent: React.FC<MyComponentProps> = ({ vertical, block }) => {
return (
<div style={{
display: block ? 'block' : 'inline',
flexDirection: vertical ? 'column' : 'row'
}}>
My Component Content
</div>
);
};
MyComponent.propTypes = {
vertical: PropTypes.bool.isRequired,
block: all(
PropTypes.bool.isRequired,
({ block, vertical }: MyComponentProps) => (
block && !vertical ?
new Error('`block` requires `vertical` to be set to have any effect') :
null
),
) as PropTypes.Validator<boolean>, // Type cast for custom validator
};
// Example usage:
// In a React application, this component would be rendered:
// import ReactDOM from 'react-dom';
// const rootElement = document.getElementById('root');
// ReactDOM.render(<MyComponent vertical={true} block={true} />, rootElement);
// <MyComponent vertical={false} block={true} /> // Will log a propTypes warning in development