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

Package detail

babel-plugin-flow-react-proptypes

brigand98.3kMIT26.0.0

converts flow types to react proptypes

react, flow, flowtype, proptypes, babel, babel-plugin, es6

readme

A babel plugin to generate React PropTypes definitions from Flow type declarations.

build status Coverage Status npm version npm downloads Dependency Status

Example

With this input:

var React = require('react');

export type Qux = {baz: 'literal'};

import type SomeExternalType from './types';

type FooProps = {
  an_optional_string?: string,
  a_number: number,
  a_boolean: boolean,
  a_generic_object: Object,
  array_of_strings: Array<string>,
  instance_of_Bar: Bar,
  anything: any,
  mixed: mixed,
  one_of: 'QUACK' | 'BARK' | 5,
  one_of_type: number | string,
  nested_object_level_1: {
    string_property_1: string,
    nested_object_level_2: {
      nested_object_level_3: {
        string_property_3: string,
      },
      string_property_2: string,
    }
  },
  should_error_if_provided: void,
  intersection: {foo: string} & { bar: number } & Qux,
  some_external_type: SomeExternalType,
  some_external_type_intersection: {foo: string} & SomeExternalType,
}

export default class Foo extends React.Component {
  props: FooProps
}}

The output will be:

var React = require('react');

var babelPluginFlowReactPropTypes_proptype_Qux = {
  baz: bpfrp_PropTypes.oneOf(['literal']).isRequired
};


export default class Foo extends React.Component {}
Foo.propTypes = {
  an_optional_string: bpfrp_PropTypes.string,
  a_number: bpfrp_PropTypes.number.isRequired,
  a_boolean: bpfrp_PropTypes.bool.isRequired,
  a_generic_object: bpfrp_PropTypes.object.isRequired,
  array_of_strings: bpfrp_PropTypes.arrayOf(bpfrp_PropTypes.string.isRequired).isRequired,
  instance_of_Bar: function () {
    return (typeof Bar === 'function' ? bpfrp_PropTypes.instanceOf(Bar).isRequired : bpfrp_PropTypes.any.isRequired).apply(this, arguments);
  },
  anything: (props, propName, componentName) => {
    if (!Object.prototype.hasOwnProperty.call(props, propName)) {
      throw new Error(`Prop \`${propName}\` has type 'any' or 'mixed', but was not provided to \`${componentName}\`. Pass undefined or any other value.`);
    }
  },
  mixed: (props, propName, componentName) => {
    if (!Object.prototype.hasOwnProperty.call(props, propName)) {
      throw new Error(`Prop \`${propName}\` has type 'any' or 'mixed', but was not provided to \`${componentName}\`. Pass undefined or any other value.`);
    }
  },
  one_of: bpfrp_PropTypes.oneOf(['QUACK', 'BARK', 5]).isRequired,
  one_of_type: bpfrp_PropTypes.oneOfType([bpfrp_PropTypes.number, bpfrp_PropTypes.string]).isRequired,
  nested_object_level_1: bpfrp_PropTypes.shape({
    string_property_1: bpfrp_PropTypes.string.isRequired,
    nested_object_level_2: bpfrp_PropTypes.shape({
      nested_object_level_3: bpfrp_PropTypes.shape({
        string_property_3: bpfrp_PropTypes.string.isRequired
      }).isRequired,
      string_property_2: bpfrp_PropTypes.string.isRequired
    }).isRequired
  }).isRequired,
  should_error_if_provided: (props, propName, componentName) => {
    if (props[propName] != null) {
      throw new Error(`Invalid prop \`${propName}\` of value \`${props[propName]}\` passed to \`${componentName}\`. Expected undefined or null.`);
    }
  },
  intersection: bpfrp_PropTypes.shape({
    foo: bpfrp_PropTypes.string.isRequired,
    bar: bpfrp_PropTypes.number.isRequired,
    baz: bpfrp_PropTypes.oneOf(['literal']).isRequired
  }).isRequired,
  some_external_type: function () {
    return (typeof bpfrp_babelPluginFlowReactPropTypes_proptype_SomeExternalType === 'function' ? bpfrp_babelPluginFlowReactPropTypes_proptype_SomeExternalType.isRequired ? bpfrp_babelPluginFlowReactPropTypes_proptype_SomeExternalType.isRequired : bpfrp_babelPluginFlowReactPropTypes_proptype_SomeExternalType : bpfrp_PropTypes.shape(bpfrp_babelPluginFlowReactPropTypes_proptype_SomeExternalType).isRequired).apply(this, arguments);
  },
  some_external_type_intersection: bpfrp_PropTypes.shape(Object.assign({}, {
    foo: bpfrp_PropTypes.string.isRequired
  }, bpfrp_babelPluginFlowReactPropTypes_proptype_SomeExternalType === bpfrp_PropTypes.any ? {} : bpfrp_babelPluginFlowReactPropTypes_proptype_SomeExternalType)).isRequired
};
import bpfrp_PropTypes from 'prop-types';
export { babelPluginFlowReactPropTypes_proptype_Qux };
import { bpfrp_babelPluginFlowReactPropTypes_proptype_SomeExternalType } from './types';

Versions

Starting in 14.0.0, we output ES6 import/export statements by default. The deadCode option (explained below) will cause it to use common.js modules instead.

In version 25.0.0 and onward, we only support babel 7. Install version 24.x.x if you're using babel 6.

The reaason for the high major versions is that any change to the prop type output, including adding new checks that previously produced no output, impact users of this plugin. Check the changelog to see what's changed at each version.

Usage

This plugin searches for a React components using type declaration. Works with functional components and ES6 classes. React.createClass is not currently supported.

Install

First install the plugin:

npm install --save-dev babel-plugin-flow-react-proptypes

Also install the prop-types package. This is required for React >=15.5.0. For earlier React versions you can use version 0.21.0 of this plugin, which doesn't use the prop-types package.

npm install --save prop-types

Then add it to your babelrc:

{
  "presets": ["..."],
  "plugins": ["flow-react-proptypes"]
}

To save some bytes in production, you can also only enable it in development mode.

{
  "presets": ["..."],
  "env": {
    "development": {
      "plugins": ["flow-react-proptypes"]
    }
  }
}

deadCode

The deadCode option (disabled by default) adds a predicate to the code allowing both your propTypes definitions and potentially the entire 'prop-types' package to be excluded in certain builds. Unlike specifying this plugin in the development env, mentioned above, this also works for packages published to npm.

  "plugins": [["flow-react-proptypes", { "deadCode": true }]]

The value of true is short for process.env.NODE_ENV === 'production'. You can alternatively pass any JavaScript expression. If the expression returns a truthy value, then the propTypes will be removed. This works because e.g. webpack will subsitute the value of process.env.NODE_ENV with 'production', resulting in the condition being 'production' === 'production', and then a minifer sees that the code we're generating can't be executed, and strips it, and the require('prop-types') code out of the final bundle.

Note: In dead code mode, we use require/module.exports instead of ES6 modules.

Example of specifying a custom expression:

  "plugins": [["flow-react-proptypes", { "deadCode": "__PROD__" }]]

useESModules

The useESModules option forces this plugin to output ES6 modules, even if the deadCode option is enabled. Your bundler will be responsible for removing the dead code.

Suppression

This plugin isn't perfect. You can disable it for an entire file with this directive (including quotes):

'no babel-plugin-flow-react-proptypes';

Specifically for react-native you can disable this for files in node_modules with the ignoreNodeModules config option.

{
  "presets": ["..."],
  "plugins": [["flow-react-proptypes", { "ignoreNodeModules": true }]]
}

If you already have other plugins in plugins section. It is important to place flow-react-proptypes before the following plugins:

  • transform-class-properties
  • transform-flow-strip-types

If you're using the 'react' or 'flow' presets, you don't need to do anything special.

changelog

25.1.0

Adds support for empty type - #213

25.0.0

We now support and depend on babel 7 - [#211]

Version 24.1.2 is the latest version for users of babel 6.

#211

24.1.2

Fixes deadCode for babel 7 #154-c1

24.1.1

Fixes spread of imported type #205

24.1.0

Supports $ReadOnlyArray #203

24.0.1

Fixes rare error with propTypes spread #199

24.0.0

Handle explicit proptypes and default props #196

23.0.0

opaque types are now supported, and are treated the same as other types, even outside of the file they're defined in.

export opaque type Foo = string;

22.0.0

Support class components with intersection type annotations

type Props = { x: string };
class C extends React.Component<Props & { y: string }>{}

21.0.0

Adds support for the following:

import type {Node} from 'react';
import {type Node} from 'react':

20.1.0

Supports class syntax with no name. Minor version because it previously gave an error.

export const MyComponent = class extends React.Component<Props> {

20.0.0

Adds support for re-exporting types - #190

import type { X } from './X';
export type { X };

If you try to rename the exports, it'll skip them currently (please contribute!).

19.1.0

These previously gave errors:

type Props = {
  x: { [k: string]: number, x: string },
  y: { [k: string]: number, [l: string]: number },
}

They now fail soft.

19.0.0

Stops trying to import types from things that look like node_modules (e.g. from 'foo'). #186. File an issue if this negatively affects you, and we can consider making it configurable.

18.0.0

Supports mappings like {[key: string]: number} using PropTypes.objectOf.

The value inside array types (e.g. Array<string> or string[]) cannot be null, unless they're marked with ?.

17.1.*

Babel 7 compatibility fixes.

17.0.0

Adds basic support for interface types.

export interface Pager {
    next(number): void,
    prev(number): void,
    hasNext: boolean,
    hasPrev: boolean,
}

16.0.0

Adds support for:

export type { Foo } from './types';

15.0.0

Fixes bug with two named type imports from one module. #172

14.0.0

Switches to ES6 modules for import/export by default.

Switches to useStatic by default

class C extends Component { static propTypes = {} }

3.2.0

  • Fix issue 96 type annotations with member expressions #98

3.1.3

  • Fix error on attempted propTypes generation for non-component function (#66)

3.1.2

  • Fix bug with functions defaulting to react components (#97)

3.1.0

  • Add support for top-level propTypes assignment of imported types (#88)
  • Add support for instanceOf (see PR #6, #92)
  • Fix isRequired for imported types (if they are not functions)

3.0.0

  • Fix #75: intersection not supported
  • Fix #71: propTypes are not created if you assign props from a pre-defined type or if you import types from another file
  • Testcase for #1, known working
  • Testcase for #19, known working

2.2.1

2.2.0

  • Merge pull request #83 from mehcode/ignore-opt
  • Enforce linting (#82)

2.1.3

2.1.2

  • Update remaining react PropTypes (#81)

2.1.1

2.1.0

  • Suppress transform of any file in node_modules (#79)

2.0.0

  • allow imports from non-local packages, #62
  • Fix typo in readme "onw_of_type" -> "one_of_type" (#77)

1.2.0

  • Fix hoc (#76)

1.1.0

  • handles intersection types, #75

1.0.0

  • uses prop-types package, fixes #72

0.21.0

  • supports existential types, fixes #68

0.20.0

  • ensure 'exports' is defined before setting type exports, fixes #65

0.19.0

  • ignore imports from non-relative paths, fixes #62

0.18.2

  • Properly handle exporting named types (#60)
  • Added dependency status repo badge (#59)

0.18.1

0.18.0

  • Use package.json files field (#58)

0.17.2

0.17.1

0.17.0

  • supports () : ReactElement =>, fixes #55

0.16.0

  • traverse function contents looking for jsx, fixes #54

0.15.0

0.14.1

0.14.0

  • add support for exact types (#51)

0.13.3

0.13.2

0.13.1

0.13.0

  • adds suppression directive, #9

0.12.2

0.12.1

  • Add support for mixed (#46)

0.12.0

  • handles GenericTypeAnnotation in getPropsForTypeAnnotation, fixes #42

0.11.0

0.10.2

  • Fixed crash when props are annotated as Object (#41)

0.10.1

  • use any for IntersectionTypeAnnotation, fixes #40

0.10.0

  • Merge pull request #39 from laat/stateless-arrow-body

0.9.6

  • Merge pull request #38 from skovhus/export-declartion-fix

0.9.5

0.9.4

  • Merge pull request #37 from skovhus/props-inline-in-class
  • Reuse typeannotation logic to fix bug with inline props (solves #35)
  • Merge pull request #36 from skovhus/travis-integration

0.9.3

0.9.2

  • Merge pull request #34 from skovhus/TupleTypeAnnotation
  • Merge pull request #33 from skovhus/support-union-with-null

0.9.1

  • Merge pull request #32 from skovhus/bug-fixes

0.9.0

  • Merge pull request #31 from skovhus/functional-components
  • Stop relying on type definition name ending with Props (solves #9)
  • Merge pull request #30 from skovhus/eslint

0.8.0

  • Merge pull request #27 from SomeHats/master

0.7.4

  • handles TypeofTypeAnnotation, fixes #25

0.7.3

  • handles StringLiteralTypeAnnotation and similar, fixes #24

0.7.2

  • handles ImportDefaultSpecifier, fixes #23

0.7.1

  • defaults import to PropTypes.any if it doesn't exist, #2

0.7.0

  • adds hacking.md, resolves #20

0.6.0

  • support string[], etc. fixes #17

0.5.2

  • fixes insertAfter with named exports and top level class declarations, fixes #16

0.5.1

  • fixes export type top level shape to use require('react'), #14

0.5.0

  • use require('react') instead of React, fixes #14

0.4.6

  • supports NullableTypeAnnotation, fixes #15

0.4.5

0.4.4

0.4.3

0.4.1

  • Merge pull request #10 from STRML/cleanup

0.4.0

  • Merge pull request #8 from STRML/void

0.3.2

0.3.1

  • fixes handling of ExportNamedDeclaration for non-TypeAlias, fixes #7

0.3.0

0.2.6

0.2.5

0.2.4

0.2.3

0.2.2

0.2.1

0.2.0

0.1.6

0.1.5

0.1.4

0.1.3

0.1.2

0.1.1