prop-types is a JavaScript library providing runtime type checking for React component props and similar object structures. It enables developers to define the expected types, shapes, and requirements for properties, issuing helpful warnings in development environments when mismatches occur. Its current stable version is 15.8.1. While once integrated directly into React, it was externalized in React v15.5 to encourage the adoption of static type checking solutions like TypeScript or Flow. prop-types continues to be maintained for projects that prefer runtime validation or are not yet using static typing, serving as a robust, albeit runtime-only, solution for ensuring data integrity within component APIs. It offers a comprehensive set of validators for primitives, instances, enums, arrays, objects, and more, making it flexible for various use cases. The library generally follows a stable release cadence, primarily for maintenance and compatibility updates rather than new features, given its mature state and the ecosystem's shift towards compile-time type checking.
npm install prop-typesVerified import paths — ran on the pinned version, not inferred.
This quickstart defines a React component `MyComponent` and illustrates the declaration of various prop types using `prop-types`, including primitive types, `instanceOf`, `oneOf`, `oneOfType`, `arrayOf`, `objectOf`, `shape`, and `exact` validators. It also shows chaining `isRequired`.
Migrate from `React.PropTypes` to `import PropTypes from 'prop-types';` (or `const PropTypes = require('prop-types');`) and use `PropTypes` directly.For compile-time type checking and stronger guarantees, consider migrating to static type checkers like TypeScript or Flow. Do not rely on prop-types for data validation in production environments.
For new projects, start with TypeScript or Flow. For existing projects, evaluate the cost-benefit of migrating away from `prop-types`.
Replace `PropTypes.shape({...})` with `PropTypes.exact({...})` when you need strict object property enforcement without allowing extras.Ensure you have `import PropTypes from 'prop-types';` (ES Modules) or `const PropTypes = require('prop-types');` (CommonJS) at the top of your file.Provide a value for the required prop when using the component, e.g., `<MyComponent myProp={someValue} />`, or ensure it's not `undefined` if optional.Pass a prop of the correct type, e.g., `<MyComponent myProp={123} />` if `myProp` expects a `number`.Add the necessary import statement for `PropTypes` at the top of the file: `import PropTypes from 'prop-types';` or `const PropTypes = require('prop-types');`.No dependency data recorded yet.