项目作者: grncdr

项目描述 :
Automatic prop-types from TypeScript types
高级语言: TypeScript
项目地址: git://github.com/grncdr/ts-react-loader.git
创建时间: 2017-11-27T11:19:41Z
项目社区:https://github.com/grncdr/ts-react-loader

开源协议:

下载


ts-react-loader

Warning this code is proof-of-concept quality at best! If you find the idea
compelling and would like to contribute to making it something reliably
shippable, say hi.

What it does

Given a file like this:

  1. import * as React from "react";
  2. interface Props {
  3. foo: string;
  4. }
  5. class FooComponent extends React.Component<Props> {
  6. context: {
  7. store: {
  8. dispatch: (action: any) => any;
  9. };
  10. };
  11. render() {
  12. return <strong>Your foo is: {this.props.foo}</strong>;
  13. }
  14. }

This loader will emit new TypeScript with prop & context types added:

  1. import * as React from "react";
  2. import * as PropTypes from "prop-types";
  3. interface Props {
  4. foo: string;
  5. }
  6. class FooComponent extends React.Component<Props> {
  7. static propTypes = {
  8. foo: PropTypes.string.isRequired
  9. };
  10. static contextTypes = {
  11. store: PropTypes.shape({
  12. dispatch: PropTypes.func.isRequired
  13. }).isRequired
  14. };
  15. context: {
  16. store: {
  17. dispatch: Function;
  18. };
  19. };
  20. render() {
  21. return <strong>Your foo is: {this.props.foo}</strong>;
  22. }
  23. }

Look ma! no repetition!