Coder Social home page Coder Social logo

ruslanxdev / babel-plugin-ts-definitions-to-proptypes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from milesj/babel-plugin-typescript-to-proptypes

0.0 1.0 0.0 852 KB

Generate React PropTypes from TypeScript definitions.

License: MIT License

TypeScript 100.00%
typescript defenition proptypes react babel babel-plugin

babel-plugin-ts-definitions-to-proptypes's Introduction

babel-plugin-ts-definitions-to-proptypes

Build Status

A Babel plugin to generate React PropTypes from TypeScript definitinos.

Examples

// Example.d.ts
// Before
import React from 'react';

export interface Props {
  name?: string;
}

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

export const Props = {
  name: PropTypes.string
};
// Example.js
import React from 'react';
import Props from './Example.d.ts';

export class Example extends React.Component {
  static propTypes = Props;

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

Requirements

  • Babel 7+
  • TypeScript 2+

Installation

npm i -D babel-plugin-ts-definitions-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.

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

if (process.env.NODE_ENV !== 'production') {
  plugins.push('babel-plugin-ts-definitions-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

  • 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-ts-definitions-to-proptypes', { customPropTypeSuffixes: ['Shape'] }]],
};
// Before
import React from 'react';
import PropTypes from 'prop-types';

const NameShape = PropTypes.string;

interface Props {
  name?: NameShape;
}

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

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

const NameShape = PropTypes.string;

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

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

babel-plugin-ts-definitions-to-proptypes's People

Contributors

milesj avatar ruslanxdev avatar

Watchers

 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.