【发布时间】:2021-01-15 20:34:57
【问题描述】:
我收到来自TypeScript 的Property 'checked' does not exist on type 'Switch'. 消息,用于this.checked 和this.disabled createRefs。另外,在最后一行,我还收到了来自TS 的Property 'checked' does not exist on type 'Switch 警告。如何解决这些警告?
interface Props {
checked: boolean;
onChange: (checked: boolean) => void;
disabled?: boolean;
}
interface States {
checked: boolean;
disabled?: boolean;
}
export default class Switch extends React.PureComponent<Props, States> {
constructor(props: any) {
super(props);
this.checked = React.createRef(); // comment this line out to use as controlled component
this.disabled = React.createRef(); // comment this line out to use as controlled component
this.state = {
checked: false,
disabled: false,
};
}
render() {
...
<div ref={this.checked}> // TypeScript warns: Property 'checked' does not exist on type 'Switch'
【问题讨论】:
-
如果扩展
React.Component而不是React.PureComponent会怎样? -
没有帮助抱歉
-
为什么在构造函数签名中使用
any作为props类型而不是Props? -
@НиколайГольцев 好收获!
标签: reactjs typescript ref create-ref