$ cnpm install babel-plugin-react-flow-props-to-prop-types
Convert Flow React props annotation to PropTypes
Supported:
any/mixed Unknown typesvoid/null Empty typesnumber / string / boolean Primitives42 / "hello" / true Literals[1, 2, 3] Tuples{ ... } Objects
{ prop: number } Object Properties{ prop?: number } Optional properties{ [prop: string]: number } Optional Indexers{ [key: string]: number } Object indexersArray<string> ArraysObject Unknown ObjectsFunction Unknown FunctionsRegExp regular expressionsboolean | string Unions{ foo: number } & { bar: string } Intersectionstype Alias = number - Type Aliasesinterface Stuff {} - Interfacesclass Thing {} - Class Declarationsimport type {Alias} from "./other"; Type importsUnsupported:
{ a: number, [b: string]: number } Combining properties and indexers{ [a: string]: number, [b: string]: number } Multiple indexers{ (): void } Object call propertiesa.b Qualified type identifiersimport typeof Export from "./other"; Typeof importsIn:
class MyComponent extends React.Component {
props: {
// Add a class name to the root element
className: string
};
// ...
}
Out:
class MyComponent extends React.Component {
props: {
// Add a class name to the root element
className: string
};
static propTypes = {
// Add a class name to the root element
className: PropTypes.string.isRequired
};
// ...
}
$ yarn add prop-types prop-types-extra
$ yarn add --dev babel-plugin-react-flow-props-to-prop-types
Note:
prop-types-extrais necessary for intersection type support.
.babelrc (Recommended).babelrc
{
"plugins": [
["react-flow-props-to-prop-types", { /* options */ }]
]
}
$ babel --plugins react-flow-props-to-prop-types script.js
require("babel-core").transform("code", {
plugins: [
["react-flow-props-to-prop-types", { /* options */ }]
]
});
resolveOpts (optional)Passed through to node-resolve
Sometimes you have Flow types which cannot be translated into PropTypes. In these scenarios you can provide your own type:
import type {PropType} from "babel-plugin-react-flow-props-to-prop-types";
class MyComponent extends React.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.
Copyright 2013 - present © cnpmjs.org | Home |