GithubHelp home page GithubHelp logo

prop-types-to-dts's Introduction

prop-types-to-dts

Generates TypeScript definition file for React components from PropTypes

Example:

const {injectPropTypes, generateTypes} = require('prop-types-to-dts');

injectPropTypes(require('prop-types'));

const moduleName = 'my-library';
const moduleExport = require('./my-library');

const types = generateTypes(moduleName, moduleExport,{
  //Custom imports, types and interfaces. Should be: string | Array<string>
  customDeclarations: [
    //Added CSSProperties to default import 
    `import { Component, PureComponent, ReactChild, ReactNode, SFC, CSSProperties } from 'react'`,
    '',
    //Added our custom Message interface that was used as PropTypes.instanceOf(Message)
    'export interface Message {',
    '  show: (str: string) => void;',
    '}',
    '',
  ], 

  //Possible mappers:
  //mapFunction:   (path, defValue) => defValue, //for each PropTypes.func
  //mapReactChild: (path, defValue) => defValue, //for each PropTypes.element
  //mapReactNode:  (path, defValue) => defValue, //for each PropTypes.node
  //mapArray:      (path, defValue) => defValue, //for each PropTypes.array
  //mapObject:     (path, defValue) => defValue, //for each PropTypes.object
  //mapAny:        (path, defValue) => defValue, //for each PropTypes.any

  //Generate function declaration
  mapFunction: (path, def)=> /validateString/.test(path) ? '(value: string) => boolean' : def,

  //Replace all style objects with CSSProperties
  mapObject: (path, defValue) => /style/i.test(path) ? 'CSSProperties' : defValue, 

  //Callback for getting whole component type
  getComponent: (name, props, parentName, defValue) => defValue,

  //Set this true if you specified customized import of react types in customDeclarations
  skipReactImport: true, //default: false
});

require('fs').writeFileSync(moduleName+'.d.ts', types);

prop-types-to-dts's People

Contributors

evgsil 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.