当使用 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/