npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-dts-generator

v0.1.3

Published

Simple .d.ts generator for React components.

Downloads

49

Readme

react-dts-generator

Simple .d.ts generator for React components. Try with Repl.

Installation

## npm
npm install react-dts-generator
## yarn
yarn add react-dts-generator

Usage

import { generate } from 'react-dts-generator';
const result = generate(options);

Options

input: string

Path of the .js file that contains React Component. react-dts-generator use the react-docgen library to generate props and methods. The input file format guideline:

  • Modules have to export a single component, and only that component is analyzed.
  • When using React.createClass, the component definition (the value passed to it) must resolve to an object literal.
  • When using classes, the class must either extend React.Component or define a render() method.
  • propTypes must be an object literal or resolve to an object literal in the same file.
  • The return statement in getDefaultProps must contain an object literal.

output: string

The .d.ts file that contains typescript definitions. If not specified output file will be exported to the same location of the input file.

isBaseClass?: boolean

If the input component is a base class for another component the type definition could be generated with generic prop types like below. Then, another component could pass own props to the base class definition.

Input

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

class BaseClass extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.bar = this.bar.bind(this);
    }

    foo = () => { }
    bar() { }

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

BaseClass.propTypes = {
    foo: PropTypes.any,
}

export default BaseClass;

Generate

const result = generate({
	input: 'path-to-input',
	isBaseClass: true,
});

Output

import * as React from "react";

export interface BaseClassProps {
  foo?: any;
}

export default class BaseClass<T = any> extends React.Component<T> {
  foo(): any;
  bar(): any;
}

extends?: { includePropsAsGeneric: boolean, import: ImportType }

If the input component inherits from another component, the base class could import from outside.

  • includePropsAsGeneric: boolean Should the props of the input component pass to the base class as generic?

  • import: ImportType Indicates where the base class located.

Input

import * as React from 'react';
import * as PropTypes from 'prop-types';
import BaseClass from './base';

class TestClass extends BaseClass {
	render() {
		return <div>TestClass</div>;
	}
}

TestClass.propTypes = {
	foo: PropTypes.any,
}

export default TestClass;

Generate

const result = generate({
	input: 'path-to-input',
	extends: {
		import: {
			default: 'BaseClass',
			from: './base',
		},
		includePropsAsGeneric: true,
	},
});

Output

import * as React from "react";
import BaseClass from "./base";

export interface TestClassProps {
  foo?: any;
}

export default class TestClass extends BaseClass<TestClassProps> {}

propTypesComposition: ImportType[]

If the component propTypes has composes by another component's propTypes, and typescript definitions of the other component were already generated they could be imported and generated types extend from them.

TestClass.propTypes = {
	...BaseClass.propTypes,
	foo: PropTypes.any,
}
const result = generate({
	input: 'path-to-input',
	propTypesComposition: [{
		named: 'BaseClass',
		from: '../base-props-path',
	}],
});

Samples

Checkout the baselines and tests.

Known Issues

  • These propTypes generated as any

    • PropTypes.symbol
    • PropTypes.elementType
    • PropTypes.instanceOf
    • PropTypes.objectOf
    • PropTypes.exact
  • Custom propTypes is not supported.