babel-plugin-react-flow-props-to-prop-types
Convert Flow React props annotation to PropTypes
- Supports most Flow types (see below)
- Maintains comments
- Works across modules (can import types)
Supported:
any/mixedUnknown typesvoid/nullEmpty typesnumber / string / booleanPrimitives42 / "hello" / trueLiterals[1, 2, 3]Tuples{ ... }Objects{ prop: number }Object Properties{ prop?: number }Optional properties{ [prop: string]: number }Optional Indexers
{ [key: string]: number }Object indexersArray<string>ArraysObjectUnknown ObjectsFunctionUnknown FunctionsRegExpregular expressionsboolean | stringUnions{ foo: number } & { bar: string }Intersections- Referencing other types:
type Alias = number- Type Aliasesinterface Stuff {}- Interfacesclass Thing {}- Class Declarationsimport type {Alias} from "./other";Type imports
Unsupported:
{ a: number, [b: string]: number }Combining properties and indexers{ [a: string]: number, [b: string]: number }Multiple indexers{ (): void }Object call propertiesa.bQualified type identifiersimport typeof Export from "./other";Typeof imports
Example
In:
Component props: // Add a class name to the root element className: string ; // ...Out:
Component props: // Add a class name to the root element className: string ; static propTypes = // Add a class name to the root element className: PropTypesstringisRequired ; // ...Installation
$ yarn add prop-types prop-types-extra$ yarn add --dev babel-plugin-react-flow-props-to-prop-typesNote:
prop-types-extrais necessary for intersection type support.
Usage
Via .babelrc (Recommended)
.babelrc
"plugins": "react-flow-props-to-prop-types" /* options */ Via CLI
$ babel --plugins react-flow-props-to-prop-types script.jsVia Node API
;Options
resolveOpts (optional)
Passed through to node-resolve
Override type used in propTypes
Sometimes you have Flow types which cannot be translated into PropTypes. In these scenarios you can provide your own type:
; Component props: foo: PropType<UnknownFunctionType Function> ;PropType is defined as:
type PropType<T R> = T;So Flow will use the first type you provide, while this Babel plugin will use the second.