javascripttypescriptjsdoc

How to use typescript jsdoc annotations for React PropTypes


When defining react components using typescript we can write something like:

class SomeComponent extends React.Component<PropInterface, StateInterface> {
  // ...
}

Is there a way do the equivalent using jsdoc annotations and have props type-checked.


Solution

  • I prefer following form (es2015 + @types/react):

    /**
     * @typedef {object} Props
     * @prop {string} className
     * @prop {number} numberProp
     *
     * @extends {Component<Props>}
     */
    export default class SomeComponent extends Component {
        render() {
            return (
                <div className={this.props.className}>
                    {this.props.numberProp}
                </div>
            );
        }
    
    }