UNPKG

react-to-typescript-definitions

Version:
90 lines (74 loc) 3.03 kB
# react-to-typescript-definitions [![GitHub license](https://img.shields.io/github/license/KnisterPeter/react-to-typescript-definitions.svg)]() [![Travis](https://img.shields.io/travis/KnisterPeter/react-to-typescript-definitions.svg)](https://travis-ci.org/KnisterPeter/react-to-typescript-definitions) [![Coveralls branch](https://img.shields.io/coveralls/KnisterPeter/react-to-typescript-definitions/master.svg)](https://coveralls.io/github/KnisterPeter/react-to-typescript-definitions) [![David](https://img.shields.io/david/KnisterPeter/react-to-typescript-definitions.svg)](https://david-dm.org/KnisterPeter/react-to-typescript-definitions) [![David](https://img.shields.io/david/dev/KnisterPeter/react-to-typescript-definitions.svg)](https://david-dm.org/KnisterPeter/react-to-typescript-definitions#info=devDependencies&view=table) [![npm](https://img.shields.io/npm/v/react-to-typescript-definitions.svg)](https://www.npmjs.com/package/react-to-typescript-definitions) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) Create typescript definitions files (d.ts) from react components. # Features * ES6 and ES7 class syntax * Most PropTypes * any, array, bool, func, number, object, string, node, element, oneOfType, arrayOf * required PropTypes * instanceOf PropTypes (when using API and giving a resolve function) * jsdoc # Usage ## Installation Install as npm package: ```sh npm install react-to-typescript-definitions --save-dev ``` or ```sh npm install -g react-to-typescript-definitions ``` ## CLI ```sh cat <some/react/component.jsx> |react2dts --name module-name ``` ## API Functions: ```js /** * Returns the typescript definition for the given file. * * @param name The name of the generated module * @param path The path to the file to parse * @param options The options to use * @return The type definition as string */ function generateFromFile(name, path, options) ``` ```js /** * Returns the typescript definition for the given source. * * @param name The name of the generated module * @param code The code to parse * @param options The options to use * @return The type definition as string */ function generateFromSource(name, code, options) ``` ```js /** * Returns the typescript definition for the given babylon AST object. * * @param name The name of the generated module * @param ast The babylon ASt to parse * @param options The options to use * @return The type definition as string */ function generateFromAst(name, ast, options) ``` Options: * generator An instance of the exported Generator class. This options is usefull to write multiple declarations into one .d.ts file. The generator could be used for consecutive function calls. * instanceOfResolver A function which gets a type name (as string) and should return the path to the file defining the type or undefined if the type is not resolvable. This function is required to generate instanceOf PropTypes.