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.
Given a file like this:
import*asReactfrom"react";interfaceProps{foo: string;}classFooComponentextendsReact.Component<Props>{context: {store: {dispatch: (action: any)=>any;};};render(){return<strong>Yourfoo is: {this.props.foo}</strong>;}}This loader will emit new TypeScript with prop & context types added:
import*asReactfrom"react";import*asPropTypesfrom"prop-types";interfaceProps{foo: string;}classFooComponentextendsReact.Component<Props>{staticpropTypes={foo: PropTypes.string.isRequired};staticcontextTypes={store: PropTypes.shape({dispatch: PropTypes.func.isRequired}).isRequired};context: {store: {dispatch: Function;};};render(){return<strong>Yourfoo is: {this.props.foo}</strong>;}}Look ma! no repetition!