javascript - 如何为 React PropTypes 使用 typescript jsdoc 注释

标签 javascript typescript jsdoc

当使用 typescript 定义 React 组件时,我们可以这样写:

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

有没有办法使用 jsdoc annotations 做同样的事情?并对 Prop 进行类型检查。

最佳答案

我更喜欢以下形式 (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>
        );
    }

}

关于javascript - 如何为 React PropTypes 使用 typescript jsdoc 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768915/

相关文章:

knockout.js - 升级到 0.9.5 后修复 knockout typescript 编译器错误的正确方法

typescript - 使用类表达式时是否支持循环引用?

javascript - 此html代码是否可以自动提交(如果提交后需要出现单独的页面)

javascript - 如何在按回车键使用 contenteditable 时多次提交表单

typescript - ts节点: SyntaxError: Cannot use import statement outside a module

Vim JSDoc、PHPDoc、JavaDoc、RDoc 信息

jsdoc - 寻找: Example of how to use jsDoc with the Webstorm IDE (v 4)

typescript - 在 Typescript 中使用 JSDoc 中定义的接口(interface)

javascript - React 子组件不会在父状态更改时重新渲染

javascript - 了解 JavaScript 运行时