GithubHelp home page GithubHelp logo

atlassian / babel-plugin-react-flow-props-to-prop-types Goto Github PK

View Code? Open in Web Editor NEW
234.0 8.0 25.0 483 KB

Convert Flow React props annotation to PropTypes

License: Other

JavaScript 100.00%
babel react flow flowtype proptypes

babel-plugin-react-flow-props-to-prop-types's Introduction

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/mixed Unknown types
  • void/null Empty types
  • number / string / boolean Primitives
  • 42 / "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 indexers
  • Array<string> Arrays
  • Object Unknown Objects
  • Function Unknown Functions
  • RegExp regular expressions
  • boolean | string Unions
  • { foo: number } & { bar: string } Intersections
  • Referencing other types:
    • type Alias = number - Type Aliases
    • interface Stuff {} - Interfaces
    • class Thing {} - Class Declarations
    • import 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 properties
  • a.b Qualified type identifiers
  • import typeof Export from "./other"; Typeof imports

Example

In:

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
  };

  // ...
}

Installation

$ yarn add prop-types prop-types-extra
$ yarn add --dev babel-plugin-react-flow-props-to-prop-types

Note: prop-types-extra is 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.js

Via Node API

require("babel-core").transform("code", {
  plugins: [
    ["react-flow-props-to-prop-types", { /* options */ }]
  ]
});

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:

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.

babel-plugin-react-flow-props-to-prop-types's People

Contributors

dependabot[bot] avatar jamiebuilds avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.