Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

prop-types

facebook119.9mMIT15.8.1TypeScript support: definitely-typed

Runtime type checking for React props and similar objects.

react

readme

prop-types Build Status

Runtime type checking for React props and similar objects.

You can use prop-types to document the intended types of properties passed to components. React (and potentially other libraries—see the checkPropTypes() reference below) will check props passed to your components against those definitions, and warn in development if they don’t match.

Installation

npm install --save prop-types

Importing

import PropTypes from 'prop-types'; // ES6
var PropTypes = require('prop-types'); // ES5 with npm

CDN

If you prefer to exclude prop-types from your application and use it globally via window.PropTypes, the prop-types package provides single-file distributions, which are hosted on the following CDNs:

  • unpkg `html <script src="https://unpkg.com/prop-types@15.6/prop-types.js"></script>
<script src="https://unpkg.com/prop-types@15.6/prop-types.min.js"></script>

* [**cdnjs**](https://cdnjs.com/libraries/prop-types)
```html
<!-- development version -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.6.0/prop-types.js"></script>

<!-- production version -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.6.0/prop-types.min.js"></script>

To load a specific version of prop-types replace 15.6.0 with the version number.

Usage

PropTypes was originally exposed as part of the React core module, and is commonly used with React components. Here is an example of using PropTypes with a React component, which also documents the different validators provided:

import React from 'react';
import PropTypes from 'prop-types';

class MyComponent extends React.Component {
  render() {
    // ... do things with the props
  }
}

MyComponent.propTypes = {
  // You can declare that a prop is a specific JS primitive. By default, these
  // are all optional.
  optionalArray: PropTypes.array,
  optionalBigInt: PropTypes.bigint,
  optionalBool: PropTypes.bool,
  optionalFunc: PropTypes.func,
  optionalNumber: PropTypes.number,
  optionalObject: PropTypes.object,
  optionalString: PropTypes.string,
  optionalSymbol: PropTypes.symbol,

  // Anything that can be rendered: numbers, strings, elements or an array
  // (or fragment) containing these types.
  // see https://reactjs.org/docs/rendering-elements.html for more info
  optionalNode: PropTypes.node,

  // A React element (ie. <MyComponent />).
  optionalElement: PropTypes.element,

  // A React element type (eg. MyComponent).
  // a function, string, or "element-like" object (eg. React.Fragment, Suspense, etc.)
  // see https://github.com/facebook/react/blob/HEAD/packages/shared/isValidElementType.js
  optionalElementType: PropTypes.elementType,

  // You can also declare that a prop is an instance of a class. This uses
  // JS's instanceof operator.
  optionalMessage: PropTypes.instanceOf(Message),

  // You can ensure that your prop is limited to specific values by treating
  // it as an enum.
  optionalEnum: PropTypes.oneOf(['News', 'Photos']),

  // An object that could be one of many types
  optionalUnion: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
    PropTypes.instanceOf(Message)
  ]),

  // An array of a certain type
  optionalArrayOf: PropTypes.arrayOf(PropTypes.number),

  // An object with property values of a certain type
  optionalObjectOf: PropTypes.objectOf(PropTypes.number),

  // You can chain any of the above with `isRequired` to make sure a warning
  // is shown if the prop isn't provided.

  // An object taking on a particular shape
  optionalObjectWithShape: PropTypes.shape({
    optionalProperty: PropTypes.string,
    requiredProperty: PropTypes.number.isRequired
  }),

  // An object with warnings on extra properties
  optionalObjectWithStrictShape: PropTypes.exact({
    optionalProperty: PropTypes.string,
    requiredProperty: PropTypes.number.isRequired
  }),

  requiredFunc: PropTypes.func.isRequired,

  // A value of any data type
  requiredAny: PropTypes.any.isRequired,

  // You can also specify a custom validator. It should return an Error
  // object if the validation fails. Don't `console.warn` or throw, as this
  // won't work inside `oneOfType`.
  customProp: function(props, propName, componentName) {
    if (!/matchme/.test(props[propName])) {
      return new Error(
        'Invalid prop `' + propName + '` supplied to' +
        ' `' + componentName + '`. Validation failed.'
      );
    }
  },

  // You can also supply a custom validator to `arrayOf` and `objectOf`.
  // It should return an Error object if the validation fails. The validator
  // will be called for each key in the array or object. The first two
  // arguments of the validator are the array or object itself, and the
  // current item's key.
  customArrayProp: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
    if (!/matchme/.test(propValue[key])) {
      return new Error(
        'Invalid prop `' + propFullName + '` supplied to' +
        ' `' + componentName + '`. Validation failed.'
      );
    }
  })
};

Refer to the React documentation for more information.

Migrating from React.PropTypes

Check out Migrating from React.PropTypes for details on how to migrate to prop-types from React.PropTypes.

Note that this blog posts mentions a codemod script that performs the conversion automatically.

There are also important notes below.

How to Depend on This Package?

For apps, we recommend putting it in dependencies with a caret range. For example:

  "dependencies": {
    "prop-types": "^15.5.7"
  }

For libraries, we also recommend leaving it in dependencies:

  "dependencies": {
    "prop-types": "^15.5.7"
  },
  "peerDependencies": {
    "react": "^15.5.0"
  }

Note: there are known issues in versions before 15.5.7 so we recommend using it as the minimal version.

Make sure that the version range uses a caret (^) and thus is broad enough for npm to efficiently deduplicate packages.

For UMD bundles of your components, make sure you don’t include PropTypes in the build. Usually this is done by marking it as an external (the specifics depend on your bundler), just like you do with React.

Compatibility

React 0.14

This package is compatible with React 0.14.9. Compared to 0.14.8 (which was released in March of 2016), there are no other changes in 0.14.9, so it should be a painless upgrade.

# ATTENTION: Only run this if you still use React 0.14!
npm install --save react@^0.14.9 react-dom@^0.14.9

React 15+

This package is compatible with React 15.3.0 and higher.

npm install --save react@^15.3.0 react-dom@^15.3.0

What happens on other React versions?

It outputs warnings with the message below even though the developer doesn’t do anything wrong. Unfortunately there is no solution for this other than updating React to either 15.3.0 or higher, or 0.14.9 if you’re using React 0.14.

Difference from React.PropTypes: Don’t Call Validator Functions

First of all, which version of React are you using? You might be seeing this message because a component library has updated to use prop-types package, but your version of React is incompatible with it. See the above section for more details.

Are you using either React 0.14.9 or a version higher than React 15.3.0? Read on.

When you migrate components to use the standalone prop-types, all validator functions will start throwing an error if you call them directly. This makes sure that nobody relies on them in production code, and it is safe to strip their implementations to optimize the bundle size.

Code like this is still fine:

MyComponent.propTypes = {
  myProp: PropTypes.bool
};

However, code like this will not work with the prop-types package:

// Will not work with `prop-types` package!
var errorOrNull = PropTypes.bool(42, 'myProp', 'MyComponent', 'prop');

It will throw an error:

Calling PropTypes validators directly is not supported by the `prop-types` package.
Use PropTypes.checkPropTypes() to call them.

(If you see a warning rather than an error with this message, please check the above section about compatibility.)

This is new behavior, and you will only encounter it when you migrate from React.PropTypes to the prop-types package. For the vast majority of components, this doesn’t matter, and if you didn’t see this warning in your components, your code is safe to migrate. This is not a breaking change in React because you are only opting into this change for a component by explicitly changing your imports to use prop-types. If you temporarily need the old behavior, you can keep using React.PropTypes until React 16.

If you absolutely need to trigger the validation manually, call PropTypes.checkPropTypes(). Unlike the validators themselves, this function is safe to call in production, as it will be replaced by an empty function:

// Works with standalone PropTypes
PropTypes.checkPropTypes(MyComponent.propTypes, props, 'prop', 'MyComponent');

See below for more info.

If you DO want to use validation in production, you can choose to use the development version by importing/requiring prop-types/prop-types instead of prop-types.

You might also see this error if you’re calling a PropTypes validator from your own custom PropTypes validator. In this case, the fix is to make sure that you are passing all of the arguments to the inner function. There is a more in-depth explanation of how to fix it on this page. Alternatively, you can temporarily keep using React.PropTypes until React 16, as it would still only warn in this case.

If you use a bundler like Browserify or Webpack, don’t forget to follow these instructions to correctly bundle your application in development or production mode. Otherwise you’ll ship unnecessary code to your users.

PropTypes.checkPropTypes

React will automatically check the propTypes you set on the component, but if you are using PropTypes without React then you may want to manually call PropTypes.checkPropTypes, like so:

const myPropTypes = {
  name: PropTypes.string,
  age: PropTypes.number,
  // ... define your prop validations
};

const props = {
  name: 'hello', // is valid
  age: 'world', // not valid
};

// Let's say your component is called 'MyComponent'

// Works with standalone PropTypes
PropTypes.checkPropTypes(myPropTypes, props, 'prop', 'MyComponent');
// This will warn as follows:
// Warning: Failed prop type: Invalid prop `age` of type `string` supplied to
// `MyComponent`, expected `number`.

PropTypes.resetWarningCache()

PropTypes.checkPropTypes(...) only console.errors a given message once. To reset the error warning cache in tests, call PropTypes.resetWarningCache()

License

prop-types is MIT licensed.

changelog

15.8.1

  • [Fix] fix crash when a custom propType return lacks .data; call hasOwnProperty properly (#370)
  • [meta] Fix formatting in CHANGELOG.md (#367)
  • [Tests] add missing test coverage (#370)
  • [Tests] convert normal it functions to arrow functions (#370)
  • [Tests] do not fail fast; add react 17 (#366)
  • [Dev Deps] update eslint

15.8.0

  • [New] add PropTypes.bigint (#365)
  • [New] oneOfType: Add expected types to warning (#198)
  • [New] Add type check for validator for 'shape' and 'exact' (#234)
  • [Fix] checkPropTypes: Friendlier message when using a type checker that is not a function (#51)
  • [Refactor] extract has (#261, #125, #124)
  • [readme] Fix branch name (master -> main) (#364)
  • [readme] Clarify usage of elementType (#335)
  • [docs] highlighted the func name (#321)
  • [docs] Typo fix in example (#300)
  • [docs] Add instructions for intentional inclusion of validation in production. (#262)
  • [docs] PropTypes.node: add link to react docs
  • [docs] Improve wording for checkPropTypes (#258)
  • [meta] Add a package sideEffects field. (#350)
  • [meta] use in-publish to avoid running the build on install
  • [deps] regenerate yarn.lock
  • [deps] update react-is (#347, #346, #345, #340, #338)
  • [eslint] enable some rules (#360)
  • [Tests] Use GH Actions (#363)
  • [Tests] Fix spelling (#318)
  • [Tests] Fixed typo: 'Any type should accept any value' (#281)
  • [Tests] fix broken tests; test the build process
  • [Dev Deps] update browserify, bundle-collapser, eslint, in-publish, react, uglifyify, uglifyjs

15.7.2

  • [Fix] ensure nullish values in oneOf do not crash (#256)
  • [Fix] move loose-envify back to production deps, for browerify usage (#203)

15.7.1

  • [Fix] avoid template literal syntax (#255, #254)

15.7.0

  • [New] Add .elementType (#211)
  • [New] add PropTypes.resetWarningCache (#178)
  • oneOf: improve warning when multiple arguments are supplied (#244)
  • Fix oneOf when used with Symbols (#224)
  • Avoid relying on hasOwnProperty being present on values' prototypes (#112, #187)
  • Improve readme (#248, #233)
  • Clean up mistaken runtime dep, swap envify for loose-envify (#204)

15.6.2

  • Remove the fbjs dependency by inlining some helpers from it (#194))

15.6.1

  • Fix an issue where outdated BSD license headers were still present in the published bundle #162

15.6.0

  • Switch from BSD + Patents to MIT license
  • Add PropTypes.exact, like PropTypes.shape but warns on extra object keys. (@thejameskyle and @aweary in #41 and #87)

15.5.10

  • Fix a false positive warning when using a production UMD build of a third-party library with a DEV version of React. (@gaearon in #50)

15.5.9

  • Add loose-envify Browserify transform for users who don't envify globally. (@mridgway in #45)

15.5.8

  • Limit the manual PropTypes call warning count because it has false positives with React versions earlier than 15.2.0 in the 15.x branch and 0.14.9 in the 0.14.x branch. (@gaearon in #26)

15.5.7

  • Critical Bugfix: Fix an accidental breaking change that caused errors in production when used through React.PropTypes. (@gaearon in #20)
  • Improve the size of production UMD build. (@aweary in 38ba18 and 7882a7)

15.5.6

Note: this release has a critical issue and was deprecated. Please update to 15.5.7 or higher.

15.5.5

Note: this release has a critical issue and was deprecated. Please update to 15.5.7 or higher.

15.5.4

Note: this release has a critical issue and was deprecated. Please update to 15.5.7 or higher.

  • Reduce the size of the UMD Build. (@acdlite in 31e9344)
  • Remove bad package url. (@ljharb in 158198f)
  • Remove the accidentally included typechecking code from the production build.

15.5.3

Note: this release has a critical issue and was deprecated. Please update to 15.5.7 or higher.

  • Remove the accidentally included React package code from the UMD bundle. (@acdlite in df318bb)

15.5.2

Note: this release has a critical issue and was deprecated. Please update to 15.5.7 or higher.

  • Remove dependency on React for CommonJS entry point. (@acdlite in cae72bb)

15.5.1

Note: this release has a critical issue and was deprecated. Please update to 15.5.7 or higher.

  • Remove accidental uncompiled ES6 syntax in the published package. (@acdlite in e191963)

15.5.0

Note: this release has a critical issue and was deprecated. Please update to 15.5.7 or higher.

  • Initial release.

Before 15.5.0

PropTypes was previously included in React, but is now a separate package. For earlier history of PropTypes see the React change log.