All Projects β†’ KuveytTurk β†’ react-dts-generator

KuveytTurk / react-dts-generator

Licence: Apache-2.0 License
Typescript definitions for React components.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-dts-generator

Definitelytyped
The repository for high quality TypeScript type definitions.
Stars: ✭ 37,066 (+205822.22%)
Mutual labels:  dts, typings
webgpu-seed
πŸ”ΊπŸŒ± An example on how to render a hello triangle with WebGPU.
Stars: ✭ 178 (+888.89%)
Mutual labels:  dts, typings
mxgraph-road-to-DefinitelyTyped
Community effort followup to provide mxgraph TypeScript type definitions / typings via DefinitelyTyped
Stars: ✭ 23 (+27.78%)
Mutual labels:  typings, definitelytyped
tiny-typed-emitter
Fully type-checked NodeJS EventEmitter
Stars: ✭ 96 (+433.33%)
Mutual labels:  typings, definitelytyped
ts
βš™οΈ The CLI that TypeScript deserves.
Stars: ✭ 17 (-5.56%)
Mutual labels:  definitelytyped
aliyun-dts-subscribe-sdk-java
aliyun-dts-subscribe-sdk-java
Stars: ✭ 18 (+0%)
Mutual labels:  dts
vite-plugin-dts
A vite plugin for generating `.d.ts` files.
Stars: ✭ 539 (+2894.44%)
Mutual labels:  dts
Converter
Typescript to Scala.js converter
Stars: ✭ 168 (+833.33%)
Mutual labels:  definitelytyped
discord-api-types
Up to date Discord API Typings, versioned by the API version
Stars: ✭ 270 (+1400%)
Mutual labels:  typings
definitelytyped-firefox-webext-browser
Script to generate TypeScript definitions for WebExtension Development in FireFox
Stars: ✭ 24 (+33.33%)
Mutual labels:  definitelytyped
android-dts-generator
A tool that generates TypeScript declaration files (.d.ts) from Jars
Stars: ✭ 71 (+294.44%)
Mutual labels:  typings
opc-types
OpenComputers and TypeScript
Stars: ✭ 18 (+0%)
Mutual labels:  typings
typings-sanctuary
*UNMAINTAINED* type definitions (for TypeScript) for JavaScript library Sanctuary
Stars: ✭ 12 (-33.33%)
Mutual labels:  typings
tadd
πŸ“Ί Installs packages and automatically finds best Typescript typings
Stars: ✭ 20 (+11.11%)
Mutual labels:  typings
python-dts-calibration
A Python package to load raw Distributed Temperature Sensing (DTS) files, perform a calibration, and plot the result.
Stars: ✭ 21 (+16.67%)
Mutual labels:  dts
mt762x-wm8960
MT762X WM8960 ALSA SoC machine driver
Stars: ✭ 19 (+5.56%)
Mutual labels:  dts
DiscordBot-Template
A boilerplate / template for discord.js bots with 100% coverage of Discord API, command handler, error handler based on https://discordjs.guide/
Stars: ✭ 129 (+616.67%)
Mutual labels:  typings
discord.d.ts
Typings for the Discord gateway.
Stars: ✭ 24 (+33.33%)
Mutual labels:  typings
types
TypeScript Type Definitions same as DefinitelyTyped.
Stars: ✭ 31 (+72.22%)
Mutual labels:  typings
dts-css-modules-loader
A small Webpack loader to generate typings for your CSS-Modules
Stars: ✭ 44 (+144.44%)
Mutual labels:  typings

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.

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