如何为React PropTypes使用Typescript jsdoc注释 [英] How to use typescript jsdoc annotations for React PropTypes

查看:77
本文介绍了如何为React PropTypes使用Typescript jsdoc注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用打字稿定义反应成分时,我们可以编写如下内容:

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

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

有没有一种方法可以使用 jsdoc注释并检查道具的类型.

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

推荐答案

我更喜欢以下形式(es2015 +

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>
        );
    }

}

这篇关于如何为React PropTypes使用Typescript jsdoc注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆