All Projects → milesj → Babel Plugin Typescript To Proptypes

milesj / Babel Plugin Typescript To Proptypes

Licence: mit
Generate React PropTypes from TypeScript interfaces or type aliases.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Babel Plugin Typescript To Proptypes

babel-plugin-transform-pipeline
Compile pipeline operator to ES5
Stars: ✭ 58 (-80.07%)
Mutual labels:  babel-plugin
babel-plugin-remove-test-ids
🐠 Babel plugin to strip `data-test-id` HTML attributes
Stars: ✭ 40 (-86.25%)
Mutual labels:  babel-plugin
Babel Plugin Module Resolver
Custom module resolver plugin for Babel
Stars: ✭ 3,134 (+976.98%)
Mutual labels:  babel-plugin
core-web
like core-js but for Web APIs (based on polyfill.io)
Stars: ✭ 34 (-88.32%)
Mutual labels:  babel-plugin
babel-plugin-transform-html-import-to-string
Turn HTML imports (and export from) into constant strings
Stars: ✭ 22 (-92.44%)
Mutual labels:  babel-plugin
babel-plugin-syntax-pipeline
Allow parsing of pipeline operator |>
Stars: ✭ 23 (-92.1%)
Mutual labels:  babel-plugin
babel-plugin-loop-optimizer
Optimizes statements such as `forEach` and `map` to for loops.
Stars: ✭ 70 (-75.95%)
Mutual labels:  babel-plugin
Graphql Let
A layer to start/scale the use of GraphQL code generator.
Stars: ✭ 282 (-3.09%)
Mutual labels:  babel-plugin
babel-plugin-display-name-custom
display name inference for your custom react component creators
Stars: ✭ 15 (-94.85%)
Mutual labels:  babel-plugin
Babel Plugin Console
Babel Plugin that adds useful build time console functions 🎮
Stars: ✭ 278 (-4.47%)
Mutual labels:  babel-plugin
babel-plugin-transform-stitches-display-name
www.npmjs.com/package/babel-plugin-transform-stitches-display-name
Stars: ✭ 12 (-95.88%)
Mutual labels:  babel-plugin
babel-plugin-transform-replace-expressions
A Babel plugin for replacing expressions with other expressions
Stars: ✭ 23 (-92.1%)
Mutual labels:  babel-plugin
Grafoo
A GraphQL Client and Toolkit
Stars: ✭ 264 (-9.28%)
Mutual labels:  babel-plugin
babel-plugin-proposal-pattern-matching
the minimal grammar, high performance JavaScript pattern matching implementation
Stars: ✭ 34 (-88.32%)
Mutual labels:  babel-plugin
Babel Plugin Import Graphql
Enables import syntax for .graphql and .gql files
Stars: ✭ 284 (-2.41%)
Mutual labels:  babel-plugin
flair
a lean, component-centric style system for React components
Stars: ✭ 19 (-93.47%)
Mutual labels:  babel-plugin
babel-plugin-transform-relay-hot
🔥 BabelRelayPlugin with hot reload
Stars: ✭ 28 (-90.38%)
Mutual labels:  babel-plugin
Effectfuljs
JavaScript embedded effects compiler
Stars: ✭ 287 (-1.37%)
Mutual labels:  babel-plugin
React Loadable
⏳ A higher order component for loading components with promises.
Stars: ✭ 16,238 (+5480.07%)
Mutual labels:  babel-plugin
Babel Blade
(under new management!) ⛸️Solve the Double Declaration problem with inline GraphQL. Babel plugin/macro that works with any GraphQL client!
Stars: ✭ 266 (-8.59%)
Mutual labels:  babel-plugin

babel-plugin-typescript-to-proptypes

Build Status npm version npm deps

A Babel plugin to generate React PropTypes from TypeScript interfaces or type aliases.

Does not support converting external type references (as Babel has no type information) without the typeCheck option being enabled.

Examples

Supports class components that define generic props.

// Before
import React from 'react';

interface Props {
  name?: string;
}

class Example extends React.Component<Props> {
  render() {
    return <div />;
  }
}

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

class Example extends React.Component {
  static propTypes = {
    name: PropTypes.string,
  };

  render() {
    return <div />;
  }
}

Function components that annotate the props argument. Also supports anonymous functions without explicit types (below).

// Before
import React from 'react';

interface Props {
  name: string;
}

function Example(props: Props) {
  return <div />;
}

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

function Example(props) {
  return <div />;
}

Example.propTypes = {
  name: PropTypes.string.isRequired,
};

And anonymous functions that are annotated as a React.SFC, React.FC, React.StatelessComponent, or React.FunctionComponent.

// Before
import React from 'react';

type Props = {
  name?: string;
};

const Example: React.FC<Props> = (props) => <div />;

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

const Example = (props) => <div />;

Example.propTypes = {
  name: PropTypes.string,
};

Requirements

  • Babel 7+
  • TypeScript 3+

Installation

yarn add --dev babel-plugin-typescript-to-proptypes
// Or
npm install --save-dev babel-plugin-typescript-to-proptypes

Usage

Add the plugin to your Babel config. It's preferred to enable this plugin for development only, or when building a library. Requires either the @babel/plugin-syntax-jsx plugin or the @babel/preset-react preset.

// babel.config.js
const plugins = [];

if (process.env.NODE_ENV !== 'production') {
  plugins.push('babel-plugin-typescript-to-proptypes');
}

module.exports = {
  // Required
  presets: ['@babel/preset-typescript', '@babel/preset-react']
  plugins,
};

When transpiling down to ES5 or lower, the @babel/plugin-proposal-class-properties plugin is required.

Options

comments (boolean)

Copy comments from original source file for docgen purposes. Requires the comments option to also be enabled in your Babel config. Defaults to false.

module.exports = {
  plugins: [['babel-plugin-typescript-to-proptypes', { comments: true }]],
};
// Before
import React from 'react';

interface Props {
  /** This name controls the fate of the whole universe */
  name?: string;
}

class Example extends React.Component<Props> {
  render() {
    return <div />;
  }
}

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

class Example extends React.Component {
  static propTypes = {
    /** This name controls the fate of the whole universe */
    name: PropTypes.string,
  };

  render() {
    return <div />;
  }
}

customPropTypeSuffixes (string[])

Reference custom types directly when they match one of the provided suffixes. This option requires the type to be within the file itself, as imported types would be automatically removed by Babel. Defaults to [].

module.exports = {
  plugins: [['babel-plugin-typescript-to-proptypes', { customPropTypeSuffixes: ['Shape'] }]],
};
// Before
import React from 'react';
import { NameShape } from './shapes';

interface Props {
  name?: NameShape;
}

class Example extends React.Component<Props> {
  render() {
    return <div />;
  }
}

// After
import React from 'react';
import { NameShape } from './shapes';

class Example extends React.Component {
  static propTypes = {
    name: NameShape,
  };

  render() {
    return <div />;
  }
}

forbidExtraProps (boolean)

Automatically wrap all propTypes expressions with airbnb-prop-types forbidExtraProps, which will error for any unknown and unspecified prop. Defaults to false.

module.exports = {
  plugins: [['babel-plugin-typescript-to-proptypes', { forbidExtraProps: true }]],
};
// Before
import React from 'react';

interface Props {
  name?: string;
}

class Example extends React.Component<Props> {
  render() {
    return <div />;
  }
}

// After
import React from 'react';
import PropTypes from 'prop-types';
import { forbidExtraProps } from 'airbnb-prop-types';

class Example extends React.Component {
  static propTypes = forbidExtraProps({
    name: PropTypes.string,
  });

  render() {
    return <div />;
  }
}

implicitChildren (bool)

Automatically include a children prop type to mimic the implicit nature of TypeScript and React.ReactNode. Defaults to false.

module.exports = {
  plugins: [['babel-plugin-typescript-to-proptypes', { implicitChildren: true }]],
};
// Before
import React from 'react';

interface Props {
  foo: string;
}

class Example extends React.Component<Props> {
  render() {
    return <div />;
  }
}

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

class Example extends React.Component {
  static propTypes = {
    foo: PropTypes.string.isRequired,
    children: PropTypes.node,
  };

  render() {
    return <div />;
  }
}

maxDepth (number)

Maximum depth to convert while handling recursive or deeply nested shapes. Defaults to 3.

module.exports = {
  plugins: [['babel-plugin-typescript-to-proptypes', { maxDepth: 3 }]],
};
// Before
import React from 'react';

interface Props {
  one: {
    two: {
      three: {
        four: {
          five: {
            super: 'deep';
          };
        };
      };
    };
  };
}

class Example extends React.Component<Props> {
  render() {
    return <div />;
  }
}

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

class Example extends React.Component {
  static propTypes = {
    one: PropTypes.shape({
      two: PropTypes.shape({
        three: PropTypes.object,
      }),
    }),
  };

  render() {
    return <div />;
  }
}

maxSize (number)

Maximum number of prop types in a component, values in oneOf prop types (literal union), and properties in shape prop types (interface / type alias). Defaults to 25. Pass 0 to disable max.

module.exports = {
  plugins: [['babel-plugin-typescript-to-proptypes', { maxSize: 2 }]],
};
// Before
import React from 'react';

interface Props {
  one: 'foo' | 'bar' | 'baz';
  two: {
    foo: number;
    bar: string;
    baz: boolean;
  };
  three: null;
}

class Example extends React.Component<Props> {
  render() {
    return <div />;
  }
}

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

class Example extends React.Component {
  static propTypes = {
    one: PropTypes.oneOf(['foo', 'bar']),
    two: PropTypes.shape({
      foo: PropTypes.number,
      bar: PropTypes.string,
    }),
  };

  render() {
    return <div />;
  }
}

strict (boolean)

Enables strict prop types by adding isRequired to all non-optional properties. Disable this option if you want to accept nulls and non-required for all prop types. Defaults to true.

module.exports = {
  plugins: [['babel-plugin-typescript-to-proptypes', { strict: true }]],
};
// Before
import React from 'react';

interface Props {
  opt?: string;
  req: number;
}

class Example extends React.Component<Props> {
  render() {
    return <div />;
  }
}

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

class Example extends React.Component {
  static propTypes = {
    opt: PropTypes.string,
    req: PropTyines.number.isRequired,
  };

  render() {
    return <div />;
  }
}

typeCheck (boolean|string)

NOT FINISHED Resolve full type information for aliases and references using TypeScript's built-in type checker. When enabled with true, will glob for files using ./src/**/*.ts. Glob can be customized by passing a string. Defaults to false.

Note: This process is heavy and may increase compilation times.

module.exports = {
  plugins: [['babel-plugin-typescript-to-proptypes', { typeCheck: true }]],
};
// Before
import React from 'react';
import { Location } from './types';

interface Props {
  location?: Location;
}

class Example extends React.Component<Props> {
  render() {
    return <div />;
  }
}

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

class Example extends React.Component {
  static propTypes = {
    location: PropTypes.shape({
      lat: PropTypes.number,
      long: PropTypes.number,
    }),
  };

  render() {
    return <div />;
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].